Java Logging: Logger

Table of Contents

The java.util.Logger class is the main access point to the Java logging API. Here is how you create a logger:

Logger logger = Logger.getLogger("myLogger");

  

The string passed as parameter to the getLogger() factory method is the name of the Logger to create. You can choose the name freely, but the name implies where the Logger is located in the Logger hierarchy. Every . (dot) in the name is interpreted as a branch in the hierarchy. Look at these names:

myApp
myApp.user
myApp.admin
myApp.admin.import.user

These names are all valid. They also imply a hierarchy. The name "myApp" is at the top of the hierarchy. The two names "myApp.user" and "myApp.admin" are children of the "myApp" name. The name "myApp.admin.import.user" is a branch of the name "myApp.admin.import", which is again a branch of the "myApp.admin" name.

The Logger hierarchy is explored in more detail in its own text.

You can obtain the name of a Logger using the getName() method, in case you need it. Here is an example:

String name = logger.getName();

  

It is convention to use the class name of the class creating the Logger, including package name, as name for theLogger. Here is an example:

Logger logger = Logger.getLogger( MyClass.class.getName() );

  

Logging Messages

To log a message using a Logger, you call one of its many logging methods. Among these are:

log  (Level level, String message);
log  (Level level, String message, Object param1);
log  (Level level, String message, Object[] params);

log  (Level level, String message, Throwable t);

log  (LogRecord record);

logp (Level level, String sourceClass, String sourceMethod, String msg);
logp (Level level, String sourceClass, String sourceMethod, String msg,
    Object param1);
logp (Level level, String sourceClass, String sourceMethod, String msg,
    Object[] params);
logp (Level level, String sourceClass, String sourceMethod, String msg,
    Throwable t);

logrb(Level level, String sourceClass, String sourceMethod,
    String bundle, String msg);
logrb(Level level, String sourceClass, String sourceMethod,
    String bundle, String msg, Object param1);
logrb(Level level, String sourceClass, String sourceMethod,
    String bundle, String msg, Object[] params);
logrb(Level level, String sourceClass, String sourceMethod,
    String bundle, String msg, Throwable t);

entering(String sourceClass, String sourceMethod);
entering(String sourceClass, String sourceMethod, Object param1);
entering(String sourceClass, String sourceMethod, Object[] params);

exiting (String sourceClass, String sourceMethod);
exiting (String sourceClass, String sourceMethod, Object result);

fine    (String message);
finer   (String message);
finest  (String message);

config  (String message);
info    (String message);
warning (String message);
severe  (String message);

throwing(String sourceClass, String sourceMethod, Throwable t);

  

I am not going to explain every single of these methods in detail. They are explained in the JavaDoc‘s for Java. But, I am going to explain their purpose. Knowing their purpose you can most likely figure out the rest, with help from the JavaDoc.

The log() Methods

The log() group of methods will log a message at a certain log level. The log level is passed as parameter. Use one of the Level constants as parameter. Log level is covered in more detail in its own text.

Some of the log() methods can take object parameters. These object parameters are inserted into the log message, before it is being logged. The merging of object parameters into the message is only performed, if the message is not filtered out, either by a Filter, or because of too low log level. This improves performance in the cases where the message is filtered out.

Here is a log() example:

Logger logger = Logger.getLogger("myLogger");

logger.log(Level.SEVERE, "Hello logging");

  

And here is what is logged to the console (default log destination) :

08-01-2012 14:10:43 logging.LoggingExamples main
SEVERE: Hello logging

  

Here is an example that inserts a parameter into the message:

logger.log(Level.SEVERE, "Hello logging: {0} ", "P1");

  

And here is what is being logged:

08-01-2012 14:45:12 logging.LoggingExamples main
SEVERE: Hello logging: P1

  

Notice how the object parameter value P1 is inserted at the place in the log message where the {0} is located. The0 is the index of the object parameter to insert.

Here is an example that logs a message with multiple object parameters to be inserted into the log message:

logger.log(Level.SEVERE, "Hello logging: {0}, {1}",
    new Object[] {"P1", "P2"});

  

Here is what is being logged:

08-01-2012 14:45:12 logging.LoggingExamples main
SEVERE: Hello logging: P1, P2

  

Notice again how the object parameters are inserted into the log message instead of the {0} and {1} tokens. As mentioned earlier, the number inside the token refers to the index of the object parameter to insert, in the object parameter array passed to the log() message.

Here is an example that logs a Throwable:

logger.log(Level.SEVERE, "Hello logging",
    new RuntimeException("Error"));

  

Here is what is being logged (use the scrollbar to see it all):

08-01-2012 14:54:29 logging.LoggingExamples main
SEVERE: Hello logging
java.lang.RuntimeException: Error
	at logging.LoggingExamples.main(LoggingExamples.java:18)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

  

The logp() Methods

The logp() methods work like the log() methods, except each method take an extra two parameters: ThesourceClass and sourceMethod parameter.

These two parameters are intended to tell from what class and method the log message originated. In other words, which class and method was the "source" of the log message.

The logrb() Methods

The logrb() methods work like the log() methods too, except they can obtain the log messages from a resource bundle. A resource bundle is a set of texts containing key, value pairs, like a property file. Each file contains the same set of keys, with values in different languages. Resource bundles are an internationalization feature, and I won‘t cover it in great detail here.

Here is a logrb() example:

logger.logrb(Level.SEVERE, "logging.LoggingExamples", "main",
        "resources.myresources", "key1");

  

This example looks up a message in the resource bundle named resources.myresources by the key key1. If the resource bundle does not contain a key with that name (key1), the value itself is logged as a message. In this example then the value "key1" would have been logged as message, if no key named "key1" had existed in the resource bundle.

Here is what has logged:

08-01-2012 17:14:39 logging.LoggingExamples main
SEVERE: This is message 1

  

Here is what the resource bundle (property file) looks like:

key1 : This is message 1
key2 : this is message 2

  

The Java Doc says nothing about how you localize the messages using a Locale. How to select the language of the ResourceBundle, in other words.

The Last Log Methods

The Logger also has the following methods for logging:

entering(String sourceClass, String sourceMethod);
entering(String sourceClass, String sourceMethod, Object param1);
entering(String sourceClass, String sourceMethod, Object[] params);

exiting (String sourceClass, String sourceMethod);
exiting (String sourceClass, String sourceMethod, Object result);

fine    (String message);
finer   (String message);
finest  (String message);

config  (String message);
info    (String message);
warning (String message);
severe  (String message);

  

Each of these methods corresponds to a log level. For instance, finest()finer()fine()info(),warning() and severe() each corresponds to one of the log levels. Logging message using one of these methods corresponds to calling the log() method

Adding and Removing Handlers

You can add Handler‘s to the Logger using the addHandler() method. Here is an example:

Logger logger = Logger.getLogger("myLogger");

logger.addHandler(new ConsoleHandler());

logger.logrb(Level.SEVERE, "logging.LoggingExamples", "main",
        "resources.myresources", "key1");

  

Logger can have multiple Handler‘s. When logging, messages are forwarded to all Handler‘s.

You can obtain all Handler‘s of a Logger using the getHandlers() method, like this:

Handler[] handlers = logger.getHandlers();

  

You can remove a Handler using the removeHandler() method. Here is an example:

Logger logger = Logger.getLogger("myLogger");

Handler handler = new ConsoleHandler();

logger.addHandler(handler);

logger.remove(handler)

  

Setting a Log Filter

You can set filters on a Logger which filters out what LogRecords that gets forwarded to the Handler‘s of theLogger. You set the Filter on a Logger using the setFilter() method, like this:

Filter filter = new MyFilterImpl();

logger.setFilter(filter);

  

The class MyFilterImpl should be your own implementation of the Filter interface. See the text on Filters to learn more about implementing your own Filter.

You can obtain the Filter in use by calling the getFilter() method, like this:

Filter filter = logger.getFilter();

  

Setting the Log Level

You can set the minimum log level of messages to be forwarded to the Handler‘s. Here is a code example:

Logger logger = Logger.getLogger("myLogger");

logger.setLevel(Level.INFO);

  

This example sets the minimum log level of messages to be forwarded, to Level.INFO. To see the hierarchy of log levels, read the text on Log Levels.

You can obtain the log level of a Logger using the getLevel() method:

logger.getLevel();

  

You can also check if a given log level would be logged, if you tried logging a message with that log level. You do so using the isLoggable() method, like this:

boolean isInfoLoggable = logger.isLoggable(Level.INFO);

  

Parent Logger

As mentioned elsewhere in this tutorial, the Logger‘s are organized into a hierarchy. That means, that a Loggercan have a parent Logger in the hierarchy. You can obtain the parent Logger of a given Logger using thegetParent() method. Here is an example:

Logger parent = logger.getParent();

  

You can also tell the Logger to use or not use the Parent logger when logging. You do so using thesetUseParentHandlers(), like this:

logger.setUseParentHandlers(false);

  

This example switched off the forwarding of log messages to the parent Logger‘s Handler‘s.

You can check if a Logger forwards log messages to its parent Logger using the methodgetUseParentHandlers(). Here is an example:

boolean useParentLogger = logger.getUseParentHandlers();

  

Additional Methods

The Logger class has a few more methods that I haven‘t covered here. These are minor features, like obtaining theResourceBundle in use by the Logger etc. You should check out the JavaDoc‘s for the complete list of utility methods.

时间: 2024-12-21 01:31:54

Java Logging: Logger的相关文章

Java Logging: Logger Hierarchy

Table of Contents Filters and Handlers in the Logger Hierarchy Log Levels of Loggers in the Hierarchy The Logger's used in your application are typically organized into a hierarchy, as mentioned elsewhere in this tutorial. This text will take a close

【java】java自带的java.util.logging.Logger日志功能

偶然翻阅到一篇文章,注意到Java自带的Logger日志功能,特地来细细的看一看,记录一下. 1.Java自带的日志功能,默认的配置 ①Logger的默认配置,位置在JRE安装目录下lib中的logging.properties中 ②logging.properties日志文件内容如下: ############################################################ # Default Logging Configuration File # # You

java.util.logging.Logger使用具体解释

java.util.logging.Logger不是什么新奇东西了,1.4就有了,但是由于log4j的存在,这个logger一直沉默着,事实上在一些測试性的代码中,jdk自带的logger比log4j更方便. 一.创建Logger对象 static Logger getLogger(String name) 为指定子系统查找或创建一个 logger. static Logger getLogger(String name, String resourceBundleName) 为指定子系统查找或

java.util.logging.Logger基础

1. 定义 java.util.logging.Logger是Java自带的日志类,可以记录程序运行中所产生的日志.通过查看所产生的日志文件,可以分析程序的运行状况,出现异常时,分析及定位异常. 2.简单的例子 import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.Date; i

java.util.logging.Logger使用详解 (转)

http://lavasoft.blog.51cto.com/62575/184492/ ************************************************* java.util.logging.Logger使用详解 java.util.logging.Logger不是什么新鲜东西了,1.4就有了,可是因为log4j的存在,这个logger一直沉默着,其实在一些测试性的代码中,jdk自带的logger比log4j更方便. 一.创建Logger对象 static Lo

java.util.logging.Logger基础教程

从JDK1.4开始即引入与日志相关的类java.util.logging.Logger,但由于Log4J的存在,一直未能广泛使用.综合网上各类说法,大致认为: (1)Logger:适用于小型系统,当日志量过大时性能有待提升.好处在于JDK集成了此类,无需引入新包.且性能也在逐步改善当中,我认为一般而言,使用Logger即可. (2)Log4J:并发性较好,性能较强,适用于大型系统. 本文介绍java.util.logging.Logger的详细用法.    1.基本概念 Logger中有2个比较

通配置文件的方式控制java.util.logging.Logger日志输出

转自:http://zochen.iteye.com/blog/616151 简单的实现了下利用JDK中类java.util.logging.Logger来记录日志.主要在于仿照log4j方式用配置文件来配置日志的输出.网络上关于如何使用java.util.logging.Logger的文章很多,但是没有完整的如何通过配置配置文件来达到控制日志输出的资料.本文的目的在于此,欢迎拍砖.   上码... 1.首先封装了个LogManager.该类的主要作用就是static块中的代码,意在读取prop

java.util.logging.Logger 使用详解

概述: 第1部分 创建Logger对象 第2部分 日志级别 第3部分 Handler 第4部分 Formatter 第5部分 自定义 第6部分 Logger的层次关系 参考 第1部分 创建Logger对象 要使用J2SE的日志功能,首先要取得java.util.logging.Logger实例,这可以通过Logger类的两个静态getLogger()方法来取得: static Logger getLogger(String name) 查找或创建一个logger. static Logger g

java.util.logging.Logger 使用中关于时间格式的问题

java.util.logging.Logger类可以打印相关日志信息并将日志信息写入日志文件 tomcat7默认的日志格式输出方法是 java.util.logging.SimpleFormatter.format 如: Apr 11, 2018 4:11:48 PM tomcat8默认的日志格式输出方法是 org.apache.juli.OneLineFormatter 如: 12-Apr-2018 15:52:41.446 [logging.properties],该属性文件存在于java