publicclassLogExample{ privatestatic Log logger = LogFactory.getLog(LogExample.class.getName()); publicstaticvoidmain(String[] args){ System.out.println(logger.getClass()); logger.debug(“This is a debug information!”); logger.error(“This is an error information!”); logger.info(“This is an info information!”); logger.warn(“This is an warn information!”); logger.fatal(“This is a fatal information!”); } } 复制代码
控制台输出的结果:
class org.apache.commons.logging.impl.Jdk14Logger
十一月 09, 2019 7:08:56 下午 j.LogExample main
严重: This is an error information!
十一月 09, 2019 7:08:56 下午 j.LogExample main
信息: This is an info information!
十一月 09, 2019 7:08:56 下午 j.LogExample main
警告: This is an warn information!
十一月 09, 2019 7:08:56 下午 j.LogExample main
严重: This is a fatal information!
复制代码
class org.apache.commons.logging.impl.Log4JLogger
log4j:WARN No appenders could be found for logger (j.LogExample).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.复制代码
class org.apache.commons.logging.impl.Log4JLogger
[DEBUG] 2019-11-09 20:00:55,427 [main] j.LogExample.main(LogExample.java:10): This is a debug information!
[ERROR] 2019-11-09 20:00:55,430 [main] j.LogExample.main(LogExample.java:11): This is an error information!
[INFO ] 2019-11-09 20:00:55,430 [main] j.LogExample.main(LogExample.java:12): This is an info information!
[WARN ] 2019-11-09 20:00:55,430 [main] j.LogExample.main(LogExample.java:13): This is an warn information!
[FATAL] 2019-11-09 20:00:55,430 [main] j.LogExample.main(LogExample.java:14): This is a fatal information!
复制代码
publicclassLogExample{ privatestatic Log logger = LogFactory.getLog(LogExample.class.getName()); static{ PropertyConfigurator.configure(“config/log4j.my.properties”); } publicstaticvoidmain(String[] args){ System.out.println(logger.getClass()); logger.debug(“This is a debug information!”); logger.error(“This is an error information!”); logger.info(“This is an info information!”); logger.warn(“This is an warn information!”); logger.fatal(“This is a fatal information!”); } } 复制代码
/** Log4JLogger class name */privatestaticfinal String LOGGING_IMPL_LOG4J_LOGGER = "org.apache.commons.logging.impl.Log4JLogger";
/** Jdk14Logger class name */privatestaticfinal String LOGGING_IMPL_JDK14_LOGGER = "org.apache.commons.logging.impl.Jdk14Logger";
/** Jdk13LumberjackLogger class name */privatestaticfinal String LOGGING_IMPL_LUMBERJACK_LOGGER = "org.apache.commons.logging.impl.Jdk13LumberjackLogger";
/** SimpleLog class name */privatestaticfinal String LOGGING_IMPL_SIMPLE_LOGGER = "org.apache.commons.logging.impl.SimpleLog";
复制代码
也就是:
Log4JLogger
Jdk14Logger
Jdk13LumberjackLogger
SimpleLog
Use the org.apache.commons.logging.Log system property to identify the requested implementation class. If Log4J is available, return an instance of org.apache.commons.logging.impl.Log4JLogger. If JDK 1.4 or later is available, return an instance of* org.apache.commons.logging.impl.Jdk14Logger. Otherwise, return an instance of org.apache.commons.logging.impl.SimpleLog.
publicclassSimpleLogExample{ privatestatic SimpleLog logger = new SimpleLog(SimpleLogExample.class.getName());
<span class="hljs-keyword">static</span> {
logger.setLevel(SimpleLog.LOG_LEVEL_DEBUG);
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
System.out.println(logger.getClass());
logger.debug(<span class="hljs-string">"This is a debug information!"</span>);
logger.error(<span class="hljs-string">"This is an error information!"</span>);
logger.info(<span class="hljs-string">"This is an info information!"</span>);
logger.warn(<span class="hljs-string">"This is an warn information!"</span>);
logger.fatal(<span class="hljs-string">"This is a fatal information!"</span>);
}
} 复制代码
运行结果:
class org.apache.commons.logging.impl.SimpleLog
[ERROR] SimpleLogExample - This is an error information!
[INFO] SimpleLogExample - This is an info information!
[WARN] SimpleLogExample - This is an warn information!
[FATAL] SimpleLogExample - This is a fatal information!
复制代码
org.apache.commons.logging.simplelog.defaultlog - Default logging detail level for all instances of SimpleLog. Must be one of ("trace", "debug", "info", "warn", "error", or "fatal"). If not specified, defaults to "info".
org.apache.commons.logging.simplelog.log.xxxxx - Logging detail level for a SimpleLog instance named "xxxxx". Must be one of ("trace", "debug", "info", "warn", "error", or "fatal"). If not specified, the default logging detail level is used.
org.apache.commons.logging.simplelog.showlogname - Set to true if you want the Log instance name to be included in output messages. Defaults to false.
org.apache.commons.logging.simplelog.showShortLogname - Set to true if you want the last component of the name to be included in output messages. Defaults to true.
org.apache.commons.logging.simplelog.showdatetime - Set to true if you want the current date and time to be included in output messages. Default is false.
org.apache.commons.logging.simplelog.dateTimeFormat - The date and time format to be used in the output messages. The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat. If the format is not specified or is invalid, the default format is used. The default format is yyyy/MM/dd HH:mm:ss:SSS zzz.