May 10, 2020
Estimated Post Reading Time ~

Logging in AEM

Global Logging
Apache Sling Logging Configuration is used to configure the root logger. This defines the global settings for logging in AEM:
  • the logging level
  • the location of the central log file
  • the number of versions to be kept
  • version rotation; either maximum size or a time interval
  • the format to be used when writing the log messages
Loggers and Writers for Individual Services
In addition to the global logging settings, AEM allows you to configure specific settings for an individual service:
  • the specific logging level
  • the location of the individual log file
  • the number of versions to be kept
  • version rotation; either maximum size or the time interval
  • the format to be used when writing the log messages
  • the logger (the OSGi service supplying the log messages)
AEM uses the following to write log messages to file:
  1. An OSGi service (logger) writes a log message.
  2. A Logging Logger takes this message and formats it according to your specification.
  3. A Logging Writer writes all these messages to the physical file that you have defined.
Create a Custom Log File
In certain circumstances, you may want to create a custom log file with a different log level. You can do this in the repository by:

If not already existing, create a new configuration folder (sling:Folder) for your project /apps/<project-name>/config.

Under /apps/<project-name>/config, create a node for the new Apache Sling Logging Logger Configuration:

Name: org.apache.sling.commons.log.LogManager.factory.config-<identifier> (as this is a Logger)
Where <identifier> is replaced by free text that you (must) enter to identify the instance (you cannot omit this information). For example, org.apache.sling.commons.log.LogManager.factory.config-MINE
Type: sling:OsgiConfig

Set the following properties on this node:
Name: org.apache.sling.commons.log.file
Type: String
Value: specify the Log File; for example, logs/myLogFile.log
Name: org.apache.sling.commons.log.names
Type: String[] (String + Multi)
Value: specify the OSGi services for which the Logger is to log messages; for example, all of the following:
  • org.apache.sling
  • org.apache.felix
  • com.day
Name: org.apache.sling.commons.log.level
Type: String
Value: specify the log level required (debug, info, warn or error); for example debug
Configure the other parameters as required:
Name: org.apache.sling.commons.log.pattern
Type: String
Value: specify the pattern of the log message as required; for example,
{0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}* [{2}] {3} {5}

This step is only necessary when a new Writer is required (i.e. with a configuration that is different to the default Writer).

Under /apps/<project-name>/config, create a node for the new Apache Sling Logging Writer Configuration:
Name: org.apache.sling.commons.log.LogManager.factory.writer-<identifier> (as this is a Writer)
As with the Logger, <identifier> is replaced by free text that you (must) enter to identify the instance (you cannot omit this information). For example, org.apache.sling.commons.log.LogManager.factory.writer-MINE
Type: sling:OsgiConfig

Set the following properties on this node:
Name: org.apache.sling.commons.log.file
Type: String
Value: specify the Log File so that it matches the file specified in the Logger;
for this example, ../logs/myLogFile.log.
Configure the other parameters as required:
Name: org.apache.sling.commons.log.file.number
Type: Long
Value: specify the number of log files you want kept; for example, 5
Name: org.apache.sling.commons.log.file.size
Type: String
Value: specify as required to control file rotation by size/date; for example, ‘.’yyyy-MM-dd

Read your new log file with your chosen tool.
The log file created by this example will be ../crx-quickstart/logs/myLogFile.log.

The Felix Console also provides information about Sling Log Support at ../system/console/slinglog; for example http://localhost:4502/system/console/slinglog.

Apache Sling Logging Configuration
Configure:

  • Log Level and Log File, to define the location and log level of the central logging configuration (error.log). The level can be set to one of DEBUG, INFO, WARN, ERROR, and FATAL.
  • A number of Log Files and Log File Threshold to define the size and version rotation of the log file.
  • Message Pattern defines the format of the log messages.
For further information see AEM Logging and Sling Logging.

Apache Sling Logging Logger Configuration (Factory Configuration)
Configure:

  • Log Level, Log File, and Message Format to define details of the log file and messages.
  • Logger to define the category; for example, only log for com.day.cq.
  • By using Factory Configurations, any number of additional configurations can be added to cater to the various log levels and categories needed.
  • Such configurations are helpful during development; for example, to log TRACE messages for a specific service in a specific log file.
  • Such configurations are helpful in a production environment; for example, to have messages about a specific service logged to an individual log file for easier monitoring.
For further information see AEM Logging and Sling Logging

Apache Sling Logging Writer Configuration (Factory Configuration)
Configure:

  • Log File to define the existence of a log file.
  • Number of Log Files to define the version rotation.
  • The writer can be used by an Apache Sling Logging Logger Configuration configuration.
  • Such configurations are helpful during development; for example, to log TRACE messages for a specific service in a specific log file.
  • Such configurations are helpful in a production environment; for example, to have messages about a specific service logged to an individual log file for easier monitoring.
Logging in AEM is based on Sling principles
\crx-quickstart\logs
  1. access.log: All access requests to CQ WCM and the repository are registered here.
  2. error.log: Error messages (of varying levels of severity) are registered here.
  3. request.log: Each access request is registered here together with the response. 
  4. server.log: All actions made by the server are registered here.
  5. stderr.log: Holds error messages, again of varying levels of severity, generated during startup.
  6. stdout.log: Holds logging messages indicating events during startup.


By aem4beginner

No comments:

Post a Comment

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