日志手段
==============log4j日志使用===================
#1、加载依赖包
1. slf4j-api-1.5.11.jar
2. slf4j-log4j12-1.5.11.jar
3. log4j-1.2.15.jar
#2、log4j 配置文件 及 加载方法
log4j.properties=》示例:
log4j.rootLogger=info, lwtest, stdout //第一个参数表示日志级别,后面的参数都是让Log知道输出到哪里, //如果你想让日志输出到两个地方就加两个输出参数
log4j.appender.lwtest=org.apache.log4j.DailyRollingFileAppender
log4j.appender.lwtest.DatePattern='.'yyyy-MM-dd
log4j.appender.lwtest.File=logs/lwtest.log
log4j.appender.lwtest.layout=org.apache.log4j.PatternLayout
log4j.appender.lwtest.layout.ConversionPattern=%d - %m%n
log4j.appender.lwtest.Append=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n
加载配置函数:
PropertyConfigurator.configure("path")
//init log4j and watch it's change every 30seconds
PropertyConfigurator.configureAndWatch("proc/conf/log4j.properties", 30000)
#3、写抽象类
abstract class logger{
Logger logger=LoggerFactory.getLogger(this.getClass)
}
#4、使用
继承logger 类 使用logger对象即可
========================临时日志=======================================
String filePath="/home/log.txt";
public static void writeFileAppend(String filePath, String str) {
try {
PrintWriter out = new PrintWriter(new FileWriter(filePath,true));
out.print(str);
out.print("\n");
out.close();
} catch (IOException e) {
}
}