1.在服务中启动运行
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.apache.log4j.Logger;
public class GetVoiceService {
//private Logger log = LoggerFactory.getLogger(GetVoiceService.class);
private static Logger log = Logger.getLogger(com.qzlink.robot.service.GetVoiceService.class);
long start = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date()));
log.info("time:" + start);
}
配置:log4j.properties
log4j.rootLogger=info,file
#log4j.rootLogger=INFO, Console
#log4j.rootLogger=info,file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=/usr/local/voice_robot/web/ROOT/cdr_log/test/idslog_timing.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %c,%M,%F,%L %p - %m%n
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=(%r ms) [%t] %-5p: %c#%M %x: %m%n
#log4j.logger.com.genuitec.eclipse.sqlexplorer=DEBUG
#log4j.logger.org.apache=WARN
#log4j.logger.org.hibernate=WARN
原有的:
log4j.rootLogger=INFO, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=(%r ms) [%t] %-5p: %c#%M %x: %m%n
#log4j.logger.com.genuitec.eclipse.sqlexplorer=DEBUG
#log4j.logger.org.apache=WARN
#log4j.logger.org.hibernate=WARN
2.在控制台运行
import org.apache.log4j.Logger;
public class TestLog {
//public static Logger log3 = LoggerFactory.getLogger(com.qzlink.test.TestLog.class);
public static String str2;
// //获取日志记录器Logger,名字为本类类名
// public static Logger log2 = LoggerFactory.getLogger(com.qzlink.test.TestLog.class);
private static Logger log3 = Logger.getLogger(com.qzlink.test.TestLog.class);
public static void main(String[] args) throws Exception {
System.out.println("===================");
long start = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date()));
System.out.println(start);
log3.info("time:" + start);
参考资料:
java项目用log4j写日志
网友评论