Logger

From Documentation
Revision as of 05:10, 5 February 2016 by Hawk (talk | contribs) (→‎How to Configure Logging: add jdk logging reference)

In this section we describe how to configure the logging of ZK internal functions. You general can ignore it, unless you'd like to know how ZK operates internally.

Notice that, if you are using Google App Engine, you can not configure the logging as described in this chapter. For more information, please refer to Setting up Google App Engine.

How to Configure Logging

[Since ZK 7.0.0]

ZK uses SLF4J as its internal logging system, and developers can follow the SLF4J document to use the logging in ZK.

By default, ZK maven setting will bundle the slf4j-jdk14 implementation as its default logging.[1] If developer want to change that implementation for Log4j, Simple, or Logback, they can specify the following setting in their maven pom.xml file.

<dependency>
	<groupId>org.zkoss.common</groupId>
	<artifactId>zcommon</artifactId>
	<version>${zk.version}</version>
	<exclusions>
		<exclusion>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-jdk14</artifactId>
		</exclusion>
	</exclusions>
</dependency>
[Deprecated since ZK 7.0.0]

ZK uses the standard logger to log messages. You could control what to log by configuring the logging of the Web server you are using. The configuration usually varies from one server to another. However, you could use the configuration mechanism provided by ZK as described in this section. It shall work with most Web servers.

There are basically two steps to configure the standard logger with ZK's configuration mechanism:

  1. Prepare a logging configuration file
  2. Specify the configuration file in a library property

Prepare a logging configuration file

[Deprecated since ZK 7.0.0]

A logging configuration file is a standard properties file. Each line is a key-value pair in the following format:

''a.package.or.a.class'' = ''level''

Here is an example of a configuration file.

 org.zkoss.zk.ui.impl.UiEngineImpl=FINER
    #Make the log level of the specified class to FINER
 org.zkoss.zk.ui.http=DEBUG
    #Make the log level of the specified package to DEBUG
 org.zkoss.zk.ui=OFF
    #Turn off the log for the specified package
 org.zkoss=WARNING
    #Make all log levels of ZK classes to WARNING except those specified here

Allowed Levels

[Deprecated since ZK 7.0.0]
Level
Description
OFF
Indicates no message at all.
SEVERE Indicates providing error messages.
WARNING
Indicates providing warning messages. It also implies ERROR.
INFO
Indicates providing informational messages. It also implies ERROR and WARNING.
FINE Indicates providing tracing information for debugging purpose. It also implies ERROR, WARNING and INFO.
FINER
Indicates providing fairly detailed tracing information for debugging purpose. It also implies ERROR, WARNING, INFO and DEBUG

Specify the handler for Jetty and servers that don't turn on the standard logger

[Deprecated since ZK 7.0.0]

Some Web servers, such as Jetty, don't turn on the standard logger by default. Thus, in the logging configuration file, you have to configure the handler too. For example, you can turn on the java.util.logging.ConsoleHandler to write the logs to the console by adding the following lines to the logging configuration file:

handlers = java.util.logging.ConsoleHandler

java.util.logging.ConsoleHandler.level = FINER
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

Here is another example that configures the console and a file to be the target of the logs:

handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler

java.util.logging.ConsoleHandler.level = FINER
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern = /var/log/jetty6/solr-%u.log
java.util.logging.FileHandler.level = FINER

org.zkoss.zk.ui.impl.UiEngineImpl=FINER
org.zkoss.bind=FINE

Specify the configuration file in a library property

[Deprecated since ZK 7.0.0]

To let ZK load the logging configuration file, you have to specify in a library property called org.zkoss.util.logging.config.file. For example,

<library-property>
	<name>org.zkoss.util.logging.config.file</name>
	<value>conf/zk-log.properties</value>
</library-property>

If a relative path is specified, it will look for the class path first. If not found, it will assume it is related to the current directory, i.e., the directory specified in the system property called user.dir.

You could specify an absolute path, such as /usr/jetty/conf/zk-log.properties, if you are not sure what the current directory is.

Disable All Logs

If you want to disable all loggers completely or change the level for all loggers, you don't need to prepare a logging configuration file. Rather, you can configure DHtmlLayoutServlet in WEB-INF/web.xml as follows.

<servlet>
    <servlet-name>zkLoader</servlet-name>
    <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
    <init-param>
        <param-name>log-level</param-name>
        <param-value>OFF</param-value>
    </init-param>
</servlet>

For more information, please refer to ZK Configuration Reference.

How to Log

Class: Log

The logger used by ZK is based on the standard logger, java.util.logging.Logger. However, we wrap it as Log to make it more efficient.


To log the message to the client rather than the console at the server, you could use Clients.log(String)

The typical use is as follows.

 import org.zkoss.util.logging.Log;
 class MyClass {
     private static final Log log = Log.lookup(MyClass.class);
     public void f(Object v) {
         if (log.debugable()) log.debug("Value is "+v);
     }
 }

Version History

Last Update : 2016/02/05


Version Date Content
6.0.0 February 2012 LogService was deprecated.



Last Update : 2016/02/05

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.