//获取单例
FLogger logger = FLogger.getInstance();
//简便api,只需指定内容
logger.info("Here is your message...");
//指定日志级别和内容,文件名自动映射
logger.writeLog(Constant.INFO, "Here is your customized level message...");
//指定日志输出文件名、日志级别和内容
logger.writeLog("error", Constant.ERROR, "Here is your customized log file and level message...");
########## 公共环境配置 ##########
# 字符集
CHARSET_NAME = UTF-8
########## 日志信息配置 ##########
# 日志级别 0:调试信息 1:普通信息 2:警告信息 3:错误信息 4:严重错误信息
LOG_LEVEL = 0,1,2,3,4
# 日志文件存放路径
LOG_PATH =./log
# 日志写入文件的间隔时间(默认为1000毫秒)
WRITE_LOG_INV_TIME = 1000
# 单个日志文件的大小(默认为10M)
SINGLE_LOG_FILE_SIZE = 10485760
# 单个日志文件缓存的大小(默认为10KB)
SINGLE_LOG_CACHE_SIZE = 10240
[INFO] 2016-12-06 21:07:32:840 [main] Here is your message...
[WARN] 2016-12-06 21:07:32:842 [main] Here is your customized level message...
[ERROR] 2016-12-06 21:07:32:842 [main] Here is your customized log file and level message...
[日志级别] 精确到毫秒的时间 [当前线程名] 日志内容
public class LogFileItem {
/** 不包括路径,不带扩展名的日志文件名称 如:MsgInner */
public String logFileName = "";
/** 包括路径的完整日志名称 */
public String fullLogFileName = "";
/** 当前日志文件大小 */
public long currLogSize = 0;
/** 当前正在使用的日志缓存 */
public char currLogBuff = 'A';
/** 日志缓冲列表A */
public ArrayList<StringBuffer> alLogBufA = new ArrayList<StringBuffer>();
/** 日志缓冲列表B */
public ArrayList<StringBuffer> alLogBufB = new ArrayList<StringBuffer>();
/** 下次日志输出到文件时间 */
public long nextWriteTime = 0 ;
/** 上次写入时的日期 */
public String lastPCDate = "";
/** 当前已缓存大小 */
public long currCacheSize = 0;
}
//同步单个文件的日志
synchronized(lfi){
if(lfi.currLogBuff == 'A'){
lfi.alLogBufA.add(logMsg);
}else{
lfi.alLogBufB.add(logMsg);
}
lfi.currCacheSize += CommUtil.StringToBytes(logMsg.toString()).length;
}
//获得需要进行输出的缓存列表
ArrayList<StringBuffer> alWrtLog = null;
synchronized(lfi){
if(lfi.currLogBuff == 'A'){
alWrtLog = lfi.alLogBufA;
lfi.currLogBuff = 'B';
}else{
alWrtLog = lfi.alLogBufB;
lfi.currLogBuff = 'A';
}
lfi.currCacheSize = 0;
}
//创建日志文件
createLogFile(lfi);
//输出日志
int iWriteSize = writeToFile(lfi.fullLogFileName,alWrtLog);
lfi.currLogSize += iWriteSize;
# 日志写入文件的间隔时间(默认为1000毫秒)
WRITE_LOG_INV_TIME = 1000
# 单个日志文件缓存的大小(默认为10KB)
SINGLE_LOG_CACHE_SIZE = 10240
public FLogger(){
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
close();
}
}));
}
/** 线程方法 */
public void run(){
int i = 0 ;
while(bIsRun){
try{
//输出到文件
flush(false);
//重新获取日志级别
if(i++ % 100 == 0){
Constant.CFG_LOG_LEVEL = CommUtil.getConfigByString("LOG_LEVEL","0,1,2,3,4");
i = 1;
}
}catch(Exception e){
System.out.println("开启日志服务错误...");
e.printStackTrace();
}
}
}
/** 关闭方法 */
public void close(){
bIsRun = false;
try{
flush(true);
}catch(Exception e){
System.out.println("关闭日志服务错误...");
e.printStackTrace();
}
}
/**
* 输出缓存的日志到文件
* @param bIsForce 是否强制将缓存中的日志输出到文件
*/
private void flush(boolean bIsForce) throws IOException{
long currTime = System.currentTimeMillis();
Iterator<String> iter = logFileMap.keySet().iterator();
while(iter.hasNext()){
LogFileItem lfi = logFileMap.get(iter.next());
if(currTime >= lfi.nextWriteTime || SINGLE_LOG_CACHE_SIZE <= lfi.currCacheSize || bIsForce == true){
//获得需要进行输出的缓存列表
ArrayList<StringBuffer> alWrtLog = null;
synchronized(lfi){
if(lfi.currLogBuff == 'A'){
alWrtLog = lfi.alLogBufA;
lfi.currLogBuff = 'B';
}else{
alWrtLog = lfi.alLogBufB;
lfi.currLogBuff = 'A';
}
lfi.currCacheSize = 0;
}
//创建日志文件
createLogFile(lfi);
//输出日志
int iWriteSize = writeToFile(lfi.fullLogFileName,alWrtLog);
lfi.currLogSize += iWriteSize;
}
}
}
# 单个日志文件的大小(默认为10M)
SINGLE_LOG_FILE_SIZE = 10485760
info_20161207_101105.log
info_20161207_122010.log
info_20161208_011110.log
info_20161208_015010.log
info.log
/**
* 创建日志文件
* @param lfi
*/
private void createLogFile(LogFileItem lfi){
//当前系统日期
String currPCDate = TimeUtil.getPCDate('-');
//如果超过单个文件大小,则拆分文件
if(lfi.fullLogFileName != null && lfi.fullLogFileName.length() > 0 && lfi.currLogSize >= LogManager.SINGLE_LOG_FILE_SIZE ){
File oldFile = new File(lfi.fullLogFileName);
if(oldFile.exists()){
String newFileName = Constant.CFG_LOG_PATH + "/" + lfi.lastPCDate + "/" + lfi.logFileName + "_" + TimeUtil.getPCDate() + "_"+ TimeUtil.getCurrTime() + ".log";
File newFile = new File(newFileName);
boolean flag = oldFile.renameTo(newFile);
System.out.println("日志已自动备份为 " + newFile.getName() + ( flag ? "成功!" : "失败!" ) );
lfi.fullLogFileName = "";
lfi.currLogSize = 0;
}
}
//创建文件
if ( lfi.fullLogFileName == null || lfi.fullLogFileName.length() <= 0 || lfi.lastPCDate.equals(currPCDate) == false ){
String sDir = Constant.CFG_LOG_PATH + "/" + currPCDate ;
File file = new File(sDir);
if(file.exists() == false){
file.mkdir();
}
lfi.fullLogFileName = sDir + "/" + lfi.logFileName + ".log";
lfi.lastPCDate = currPCDate;
file = new File(lfi.fullLogFileName);
if(file.exists()){
lfi.currLogSize = file.length();
}else{
lfi.currLogSize = 0;
}
}
}
int i = 0;
while(bIsRun){
try{
//等待一定时间
Thread.sleep(200);
//输出到文件
flush(false);
//重新获取日志级别
if(i++ % 100 == 0){
Constant.CFG_LOG_LEVEL = CommUtil.getConfigByString("LOG_LEVEL","0,1,2,3,4");
//其他配置项热加载......
i = 1;
}
}catch(Exception e){
System.out.println("开启日志服务错误...");
e.printStackTrace();
}
}
# 日志写入文件的间隔时间
WRITE_LOG_INV_TIME = 0
# 单个日志文件的大小
SINGLE_LOG_FILE_SIZE = 104857600
# 单个日志文件缓存的大小
SINGLE_LOG_CACHE_SIZE = 0
FLogger logger = FLogger.getInstance(); //FLogger
//Logger logger = Logger.getLogger(Log4jTest.class); //log4j
String record = "Performance Testing about log4j and cyfonly customized java project log."; //72字节
long st = System.currentTimeMillis();
for(int i=0; i<1000000; i++){
logger.info(record);
}
long et = System.currentTimeMillis();
System.out.println("FLogger/log4j write 1000000 records with each record 72 bytes, cost :" + (et - st) + " millseconds");
FLogger:
[INFO] 2016-12-06 21:40:06:842 [main] Performance Testing about log4j and cyfonly customized java project log.
log4j:
[INFO ]2016-12-06 21:41:12,852, [main]Log4jTest:12, Performance Testing about log4j and cyfonly customized java project log.
FLogger write 1000000 records with each record 72 bytes, cost :2144 millseconds
log4j write 1000000 records with each record 72 bytes, cost :cost :12691 millseconds
来源:cyfonly
地址:http://www.cnblogs.com/cyfonly/p/6139049.html
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。