Logger"

From Documentation
Line 2: Line 2:
 
__TOC__
 
__TOC__
  
Class: <javadoc>org.zkoss.util.logging.Log</javadoc>
+
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.
  
The logger used by ZK is based on the standard logger, <tt>java.util.logging.Logger</tt>. However, we wrap it as <javadoc>org.zkoss.util.logging.Log</javadoc> to make it more efficient.
+
=How to Configure Logging=
  
<blockquote>
+
ZK uses [http://docs.oracle.com/javase/1.4.2/docs/guide/util/logging/overview.html 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.
----
 
To log the message to the client rather than the console at the server, you could use <javadoc method="log(java.lang.String)">org.zkoss.zk.ui.util.Clients</javadoc>
 
</blockquote>
 
  
The typical use is as follows.
+
There are basically two steps to configure the standard logger with ZK's configuration mechanism:
  
<source lang="java" >
+
# Prepare a logging configuration file
import org.zkoss.util.logging.Log;
+
# Specify the configuration file in a library property
class MyClass {
 
    private static final Log log = Log.lookup(MyClass.class);
 
    public void f(Object v) {
 
        if (log.debugable()) log.debug("Value is "+v);
 
    }
 
}
 
</source>
 
 
Since ZK uses the standard logger to log message, you could control what to log by configuring the logging of the Web server you are using. How to configure the logging of the Web server varies from one server to another. Please consult the manuals. Or, you might use the logging configuration mechanism provided by ZK as described below.
 
  
By default, all ZK log instances are mapped to the same Java logger named <tt>org.zkoss</tt> to have the better performance. If you want to control the log level for individual class, you have to call <javadoc method="setHierarchy(boolean)">org.zkoss.util.logging.Log</javadoc> to turn on the hierarchy support.
+
== Prepare a logging configuration file ==
 +
A logging configuration file is a standard properties file. Each line is a key-value pair in the following format:
  
 
<source lang="xml">
 
<source lang="xml">
Log.setHierarchy(true);
+
''a.package.or.a.class'' = ''level''
 
</source>
 
</source>
  
The hierarchy support is enabled automatically, if you configure the log level with <tt>WEB-INF/zk.xml</tt> as described in the following section.
+
Here is an example of a configuration file.
 
 
= Configure Log Levels with ZK =
 
In addition to configuring the logging of the Web server, you can use the logging configuration mechanism provided by ZK. By default, it is disabled. To enable it, you have to specify the following content in <tt>WEB-INF/zk.xml</tt>. Please refer to [[ZK Configuration Reference]] fore more details.
 
 
 
<source lang="xml" >
 
<zk>
 
    <log>
 
    <log-base>org.zkoss</log-base>
 
    </log>
 
</zk>
 
</source>
 
 
 
Alternatively, you can enable the logging configuration mechanism manually by invoking <javadoc method="init(java.lang.String, java.lang.Class)">org.zkoss.util.logging.LogService</javadoc> as follows.
 
 
 
<source lang="xml" >
 
org.zkoss.util.logging.LogService.init("org.zkoss", null);
 
</source>
 
 
If you want to log not just <tt>org.zkoss</tt> but also everything, you could specify an empty value for the <tt>log-base</tt> argument.
 
 
 
Once the mechanism is enabled, ZK will look for <tt>i3-log.conf</tt> by searching the classpath at start-up and some particular locations (see below). If found, ZK loads its content to initialize the log levels. Then, ZK keeps watching this file, and reloads its content if the file is modified.
 
 
 
= Content of i3-log.conf =
 
An example of <tt>i3-log.conf</tt> is as follows.
 
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 62: Line 27:
 
  org.zkoss.zk.ui.http=DEBUG
 
  org.zkoss.zk.ui.http=DEBUG
 
     #Make the log level of the specified package to DEBUG
 
     #Make the log level of the specified package to DEBUG
org.zkoss.zk.au.http.DHtmlUpdateServlet=INHERIT
 
    #Clear the log level of a specified class such that it inherits what
 
    #has been defined above (Default: INFO)
 
 
  org.zkoss.zk.ui=OFF
 
  org.zkoss.zk.ui=OFF
 
     #Turn off the log for the specified package
 
     #Turn off the log for the specified package
Line 71: Line 33:
 
</source>
 
</source>
 
 
== Allowed Levels ==
+
=== Allowed Levels ===
  
 
{| border="1px"
 
{| border="1px"
Line 80: Line 42:
 
| <center>OFF</center>
 
| <center>OFF</center>
 
| Indicates no message at all.
 
| Indicates no message at all.
 
 
|-
 
|-
| <center>ERROR</center>
+
| <center>ERROR | SEVERE</center>
 
| Indicates providing error messages.
 
| Indicates providing error messages.
 
 
|-
 
|-
 
| <center>WARNING</center>
 
| <center>WARNING</center>
 
| Indicates providing warning messages. It also implies ERROR.
 
| Indicates providing warning messages. It also implies ERROR.
 
 
|-
 
|-
 
| <center>INFO</center>
 
| <center>INFO</center>
 
| Indicates providing informational messages. It also implies ERROR and WARNING.
 
| Indicates providing informational messages. It also implies ERROR and WARNING.
 
 
|-
 
|-
| <center>DEBUG</center>
+
| <center>DEBUG | FINE </center>
 
| Indicates providing tracing information for debugging purpose. It also implies ERROR, WARNING and INFO.
 
| Indicates providing tracing information for debugging purpose. It also implies ERROR, WARNING and INFO.
 
 
|-
 
|-
 
| <center>FINER</center>
 
| <center>FINER</center>
 
| Indicates providing fairly detailed tracing information for debugging purpose. It also implies ERROR, WARNING, INFO and DEBUG
 
| 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 ===
 +
 +
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 <code>java.util.logging.ConsoleHandler</code> to write the logs to the console by adding the following lines to the logging configuration file:
  
|-
+
<source lang="xml">
| <center>INHERIT</center>
+
handlers = java.util.logging.ConsoleHandler
| Indicates to clear any level being set to the specified package or class. In other words, the log level will be the same as its parent node.
+
 
 +
java.util.logging.ConsoleHandler.level = FINER
 +
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
 +
</source>
 +
 
 +
Here is another example that configures the console and a file to be the target of the logs:
 +
 
 +
<source lang="xml">
 +
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
= Location of i3-log.conf =
+
java.util.logging.FileHandler.pattern = /var/log/jetty6/solr-%u.log
 +
java.util.logging.FileHandler.level = FINER
  
At first, ZK looks for this file in the classpath. If not found, it looks for the <tt>conf</tt> directory.
+
org.zkoss.zk.ui.impl.UiEngineImpl=FINER
 +
org.zkoss.bind=FINE
 +
</source>
  
{| border="1px"
+
== Specify the configuration file in a library property ==
! <center>Application Server</center>
+
To let  ZK load the logging configuration file, you have to specify in a library property called [[ZK Configuration Reference/zk.xml/The Library Properties/org.zkoss.util.logging.config.file|org.zkoss.util.logging.config.file]]. For example,
! <center>Location</center>
 
  
|-
+
<source lang="xml">
| <center>Tomcat</center>
+
<library-property>
| Place <tt>i3-log.conf</tt> under the <tt>$TOMCAT_HOME/conf</tt> directory
+
<name>org.zkoss.util.logging.config.file</name>
 +
<value>conf/zk-log.properties</value>
 +
</library-property>
 +
</source>
  
|-
+
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 <tt>user.dir</tt>.
| <center>Others</center>
 
| Try the conf directory first. If not working, you could set the system property called the <tt>org.zkoss.io.conf.dir</tt> directory to be the directory where <tt>i3-log.conf</tt> resides.
 
  
|}
+
You could specify an absolute path, such as <code>/usr/jetty/conf/zk-log.properties</code>, if you are not sure what the current directory is.
  
 
= Disable All Logs =
 
= Disable All Logs =
Some logs are generated before loading <tt>i3-log.conf</tt>. If you want to disable all logs completely, you have to either configure the logging of the Web server<ref>Remember ZK uses the standard logging utilities. Unless you specify something in <tt>i3-log.conf</tt>, and the default logging levels depend on the Web server (usually <tt>INFO</tt>).</ref>, or specify <tt>log-level</tt> when configuring <javadoc>org.zkoss.zk.ui.http.DHtmlLayoutServlet</javadoc> in <tt>WEB-INF/web.xml</tt>. For more information, please refer to [[ZK Configuration Reference/web.xml/ZK Loader|ZK Configuration Reference]].
+
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 <javadoc>org.zkoss.zk.ui.http.DHtmlLayoutServlet</javadoc> in <tt>WEB-INF/web.xml</tt> as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 137: Line 113:
 
</servlet>
 
</servlet>
 
</source>
 
</source>
 +
 +
For more information, please refer to [[ZK Configuration Reference/web.xml/ZK Loader|ZK Configuration Reference]].
 +
 +
=How to Log=
 +
Class: <javadoc>org.zkoss.util.logging.Log</javadoc>
 +
 +
The logger used by ZK is based on the standard logger, <tt>java.util.logging.Logger</tt>. However, we wrap it as <javadoc>org.zkoss.util.logging.Log</javadoc> to make it more efficient.
  
 
<blockquote>
 
<blockquote>
 
----
 
----
<references/>
+
To log the message to the client rather than the console at the server, you could use <javadoc method="log(java.lang.String)">org.zkoss.zk.ui.util.Clients</javadoc>
 
</blockquote>
 
</blockquote>
  
 +
The typical use is as follows.
 +
 +
<source lang="java" >
 +
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);
 +
    }
 +
}
 +
</source>
 +
 
=Version History=
 
=Version History=
 
{{LastUpdated}}
 
{{LastUpdated}}
Line 148: Line 143:
 
! Version !! Date !! Content
 
! Version !! Date !! Content
 
|-
 
|-
| &nbsp;
+
| 6.0.0
| &nbsp;
+
| February 2012
| &nbsp;
+
| LogService was deprecated.
 
|}
 
|}
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Revision as of 11:30, 8 February 2012

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.

How to Configure Logging

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

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

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

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

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 : 2012/02/08


Version Date Content
6.0.0 February 2012 LogService was deprecated.



Last Update : 2012/02/08

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