JAva logger使用
4333 ワード
java.util.logging.Logger
org.apache.commons.logging.Log
log4j.properties
package com.util_logging;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class TestUtilLogger {
public static void main(String[] args) throws Exception {
//test();
// test2();
test3();
}
private static void test3() throws Exception {
Logger log = Logger.getLogger("lavasoft");
log.setLevel(Level.INFO);
Logger log1 = Logger.getLogger("lavasoft");
System.out.println(log == log1); // true
Logger log2 = Logger.getLogger("lavasoft.blog");
// log2.setLevel(Level.WARNING);
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
log.addHandler(consoleHandler);
FileHandler fileHandler = new FileHandler("C:/testlog%g.log");
fileHandler.setLevel(Level.INFO);
fileHandler.setFormatter(new MyLogHander());
log.addHandler(fileHandler);
log.info("aaa");
log2.info("bbb");
log2.fine("fine");
}
static class MyLogHander extends Formatter {
@Override
public String format(LogRecord record) {
return record.getLevel() + ":" + record.getMessage() + "\r
";
}
}
private static void test2() {
Logger log = Logger.getLogger("lavasoft");
log.setLevel(Level.INFO);
Logger log1 = Logger.getLogger("lavasoft");
System.out.println(log == log1); // true
Logger log2 = Logger.getLogger("lavasoft.blog");
log2.setLevel(Level.WARNING);
log.info("aaa");
log2.info("bbb");
log2.fine("fine");
}
private static void test() throws Exception {
Logger log = Logger.getLogger("lavasoft");
log.setLevel(Level.INFO);
Logger log1 = Logger.getLogger("lavasoft");
System.out.println(log == log1); // true
Logger log2 = Logger.getLogger("lavasoft.blog");
// log2.setLevel(Level.WARNING);
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
log.addHandler(consoleHandler);
FileHandler fileHandler = new FileHandler("C:/testlog%g.log");
fileHandler.setLevel(Level.INFO);
fileHandler.setFormatter(new SimpleFormatter());
log.addHandler(fileHandler);
log.info("aaa");
log2.info("bbb");
log2.fine("fine");
}
}
org.apache.log4j.Logger package com.log4j;
import java.io.File;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class TestLog4j {
static Logger logger = Logger.getLogger(TestLog4j.class);
public static void main(String args[]) {
System.out.println(new File(".").getAbsolutePath());
PropertyConfigurator.configure("src/log4j.properties");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}
org.apache.commons.logging.Log
package com.common_log;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Commons_logging extends Object {
public static Log log = LogFactory.getLog(Commons_logging.class);
public static void test() {
log.debug("111");
log.info("125");
log.warn("485");
log.error("error");
}
public static void main(String[] a) {
Commons_logging.test();
}
}
log4j.properties
log4j.rootCategory=all,stdout,search
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyy-MM-dd HH\:mm\:ss} %p - %m%n
log4j.appender.search=org.apache.log4j.RollingFileAppender
log4j.appender.search.File=c\:/search.log
log4j.appender.search.MaxFileSize=1000KB
log4j.appender.search.MaxBackupIndex=1
log4j.appender.search.layout=org.apache.log4j.PatternLayout
log4j.appender.search.layout.ConversionPattern=%d{yyy-MM-dd HH\:mm\:ss} %p - %m%n