March 29, 2020
Estimated Post Reading Time ~

Overview of AEM Logs in Detail Part-2

AEM provided logs are not sufficient for us sometimes. We want separate logs for our application, so that it will be easy to debug our code.So In the blog we will discuss how to create custom loggers in AEM.

1. Go to felix console configMgr.
2. Go to Apache Sling Logging Logger Configuration.
3. Click on + to add a new configuration.
Log level: The log level defines which type of logs you want to have in your custom log file for ex: trace,debug,info,warn,error.
Log File:Here you can identify the name of the log file,which needs to be created under the logs folder.
Message Pattern:The java.util.MessageFormat pattern to use for formatting log messages with the root logger. This is a java.util.MessageFormat pattern supporting up to six arguments:
{0} The timestamp of type java.util.Date,
{1} the log marker,
{2} the name of the current thread,
{3} the name of the logger,
{4} the log level and
{5} the actual log message.


If the log call includes a Throwable, the stacktrace is just appended to the message regardless of the pattern.
The Pattern can be like this: {0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}* {2} {3} {5}
Logger: Here we define the package name of the application,for which we want to print the logs.You can add multiple packages also.
Additivity: If set to false then logs from these loggers would not be sent to any appender attached higher in the hierarchy.
Let's suppose we have one logging configuration for package “com.sgaem.* “and we have created one more logger for “com.sgaem.servlet.TestServlet”, so by default the TestServlet loggers will not get printed in com.sgaem.* logger file but if you enable the additivity then the loggers will go in both log files

4. Go to Apache Sling Logging Writer Configuration.
5. Click on + to add a new configuration.

Log File: Here you define the log file Name for which you want to write a logging writer.
Number of Log Files:Here you can define how many logs file you want to maintain for the specific Log File defined above.
Log File Threshold: Defines how the log file is rotated (by schedule or by size) and when to rotate.Meaning you can define if you want to rotate the log file on daily,weekly or monthly basis or on the basis of size.

Log File Rotation: Log files can grow rather quickly and fill up available disk space. To cope with this growth log files may be rotated in two ways: At specific times or when the log file reaches a configurable size. The first method is called Scheduled Rotation and is used by specifying a SimpleDateFormat pattern as the log file "size". The second method is called Size Rotation and is used by setting a maximum file size as the log file size.

Scheduled Rotation: The rolling schedule is specified by setting the org.apache.sling.commons.log.file.size property to a java.text.SimpleDateFormat pattern. 

It is possible to specify monthly, weekly, half-daily, daily, hourly, or minutely rollover schedules.Check this section to see available Scheduled Rotation. Do not use the colon ":" character anywhere in the pattern option. The text before the colon is interpreted as the protocol specification of a URL which is probably not what you want.

Size Rotation: Logfile rotation by size is specified by setting the org.apache.sling.commons.log.file.size property to a plain number or a number plus a size multiplier. The size multiplier may be any of K, KB, M, MB, G, or GB where the case is ignored and the meaning is probably obvious.

Important to Know: Apache Sling Logging logger Configuration is self-sufficient to print the log then the question can come in your mind, what is the need of Apache Sling Logging Writer Configuration.

There is one more configuration in AEM, which is Apache Sling Logging Configuration which can be considered a global configuration for all log files. Here you define a few things like Number of Log Files, Log File Threshold, Message Pattern, etc.

If you don’t override your values in your application related Logging Logger and Writer Configuration, it will take the value from Global. So if you don't define writer configuration it will take Number of Log Files and Threshold Message from global configuration.

Conclusion: if you really need any change in the two properties(Number of log files and Threshold Message) defined in the logging Writer then only create a writer configuration else no need.

There are two ways to see the log files in AEM.
1. Go to the Felix console ->Sling->Log Support, and search for your log file. You can directly open the log file from here.

2. The logs files also get created under crx-quickstart/logs. You can open your log files and see the logs.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.