Monday, January 27, 2014

Logback and SLF4jJ(another)

Logback default level is DEBUG:
TRACE < DEBUG < INFO <  WARN < ERROR.
Below requires: slf4j-api-1.7.5.jar, logback-core-1.0.13.jar and logback-classic-1.0.13.jar
Except code that configures logback (if such code exists) client code does not need to depend on logback.
Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld1");
logger.debug("Hello world.");
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
StatusPrinter.print(lc);
Calling the LoggerFactory.getLogger method with the same name will always return a reference to the exact same logger object.
Thus, it is possible to configure a logger and then to retrieve the same instance somewhere else in the code without passing around references.
appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all
enabled logging requests will at least print on the console.

A two argument variant is also available. For example, you can write:
logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);
If three or more arguments need to be passed, an Object[] variant is also available. For example, you can write:
Object[] paramArray = {newVal, below, above};
logger.debug("Value {} was inserted between {} and {}.", paramArray);

the initialization steps that logback follows to try to configure itself:
1. Logback tries to find a file called logback.groovy in the classpath.
2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
3. If no such file is found, it checks for the file logback.xml in the classpath..
4. If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
existing log4j users can convert their log4j.properties files to logback.xml using our PropertiesTranslator web-application
Here is a configuration file equivalent to the one established by BasicConfigurator:
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

You may specify the location of the default configuration file with a system property named "logback.configurationFile".
The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
Note that the file extension must be ".xml" or ".groovy". Other extensions are ignored.

If instructed to do so, logback-classic will scan for changes in its configuration file and automatically
reconfigure itself when the configuration file changes.
<configuration scan="true">
  ...
</configuration>
By default, the configuration file will be scanned for changes once every minute.
<configuration scan="true" scanPeriod="30 seconds" >
  ...
</configuration>

The below example, the appender named FILE is attached to the chapters.configuration.Foo logger.
Moreover, the chapters.configuration.Foo logger has its additivity flag set to false such that its logging
output will be sent to the appender named FILE but not to any appender attached higher in the hierarchy.
<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>foo.log</file>
    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file : %line] %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <logger name="chapters.configuration.Foo" additivity="false">
    <appender-ref ref="FILE" />
  </logger>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

The SMTPAppender relies on the JavaMail API. It has been tested with JavaMail API version 1.4.
By default, the outgoing message will contain the last 256 messages seen by SMTPAppender. If
your heart so desires, you may set a different buffer size as shown in the next example.
<configuration>  
  <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
    <smtpHost>smtp.gmail.com</smtpHost>
    <smtpPort>465</smtpPort>
    <SSL>true</SSL>
    <username>YOUR_USERNAME@gmail.com</username>
    <password>YOUR_GMAIL_PASSWORD</password>
    <to>${to}</to>
    <from>${from}</from>
    <subject>%logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.html.HTMLLayout"/>

    <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
      <!-- send just one log entry per email -->
      <bufferSize>1</bufferSize>
    </cyclicBufferTracker>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="EMAIL" />
  </root>
</configuration>

No comments:

Post a Comment