Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将日志级别更改为枚举 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/main/java/com/cyfonly/flogger/FLogger.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cyfonly.flogger;

import com.cyfonly.flogger.constants.Constant;
import com.cyfonly.flogger.constants.Constant.LogLevel;
import com.cyfonly.flogger.strategy.LogManager;
import com.cyfonly.flogger.utils.CommUtil;
import com.cyfonly.flogger.utils.TimeUtil;
Expand All @@ -21,7 +22,7 @@ public class FLogger {

public FLogger(){
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
// @Override
public void run() {
close();
}
Expand All @@ -40,48 +41,48 @@ public static synchronized FLogger getInstance(){
* @param logMsg 日志内容
*/
public void debug(String logMsg){
writeLog("debug",Constant.DEBUG,logMsg);
writeLog("debug",LogLevel.DEBUG,logMsg);
}

/**
* 写普通日志
* @param logMsg 日志内容
*/
public void info(String logMsg){
writeLog("info",Constant.INFO,logMsg);
writeLog("info",LogLevel.INFO,logMsg);
}

/**
* 写警告日志
* @param logMsg 日志内容
*/
public void warn(String logMsg){
writeLog("warn",Constant.WARN,logMsg);
writeLog("warn",LogLevel.WARN,logMsg);
}

/**
* 写错误日志
* @param logMsg 日志内容
*/
public void error(String logMsg){
writeLog("error",Constant.ERROR,logMsg);
writeLog("error",LogLevel.ERROR,logMsg);
}

/**
* 写严重错误日志
* @param logMsg 日志内容
*/
public void fatal(String logMsg){
writeLog("fatal",Constant.FATAL,logMsg);
writeLog("fatal",LogLevel.FATAL,logMsg);
}

/**
* 写系统日志
* @param level 日志级别
* @param logMsg 日志内容
*/
public void writeLog(int level,String logMsg){
writeLog(Constant.LOG_DESC_MAP.get(String.valueOf(level)).toLowerCase(),level,logMsg);
public void writeLog(LogLevel level,String logMsg){
writeLog(level.name(),level,logMsg);
}

/**
Expand All @@ -90,11 +91,11 @@ public void writeLog(int level,String logMsg){
* @param level 日志级别
* @param logMsg 日志内容
*/
public void writeLog(String logFileName, int level, String logMsg){
if(logMsg != null && Constant.CFG_LOG_LEVEL.indexOf(""+level) >= 0){
public void writeLog(String logFileName, LogLevel level, String logMsg){
if(logMsg != null && CommUtil.contains(level)){
StringBuffer sb = new StringBuffer(logMsg.length() + 100);
sb.append("[");
sb.append(Constant.LOG_DESC_MAP.get(String.valueOf(level)));
sb.append(level.name());
sb.append("] ");
sb.append(TimeUtil.getFullDateTime());
sb.append(" [");
Expand All @@ -105,7 +106,7 @@ public void writeLog(String logFileName, int level, String logMsg){
logManager.addLog(logFileName, sb);

//错误信息强制打印到控制台;若 CONSOLE_PRINT 配置为 true,也将日志打印到控制台
if(Constant.ERROR == level || Constant.FATAL == level || Constant.CONSOLE_PRINT){
if(LogLevel.ERROR == level || LogLevel.FATAL == level || Constant.CONSOLE_PRINT){
try{
System.out.print(new String(sb.toString().getBytes(Constant.CFG_CHARSET_NAME),Constant.CFG_CHARSET_NAME));
}catch(Exception e){
Expand Down
27 changes: 7 additions & 20 deletions src/main/java/com/cyfonly/flogger/constants/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
public final class Constant {

//------------------日志类型
/** 调试信息 */
public final static int DEBUG = 0;
/** 普通信息 */
public final static int INFO = 1;
/** 警告信息 */
public final static int WARN = 2;
/** 错误信息 */
public final static int ERROR = 3;
/** 严重错误信息 */
public final static int FATAL = 4;
public static enum LogLevel {
DEBUG,/** 调试信息 */
INFO,/** 普通信息 */
WARN,/** 警告信息 */
ERROR,/** 错误信息 */
FATAL/** 严重错误信息 */
}

/**日志级别*/
public static String CFG_LOG_LEVEL = CommUtil.getConfigByString("LOG_LEVEL","0,1,2,3,4");
Expand All @@ -34,14 +31,4 @@ public final class Constant {
/**日志文件路径*/
public static String CFG_LOG_PATH = CommUtil.getConfigByString("LOG_PATH","./log");

/** 日志类型描述map*/
@SuppressWarnings("serial")
public static Map<String, String> LOG_DESC_MAP = new HashMap<String, String>(){{
put("0","DEBUG");
put("1","INFO");
put("2","WARN");
put("3","ERROR");
put("4","FATAL");
}};

}
20 changes: 20 additions & 0 deletions src/main/java/com/cyfonly/flogger/utils/CommUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashMap;
import java.util.Properties;
import com.cyfonly.flogger.constants.Constant;
import com.cyfonly.flogger.constants.Constant.LogLevel;

/**
* 公用工具类
Expand Down Expand Up @@ -181,4 +182,23 @@ public static String getExpStack(Exception e) {
return bo.toString();
}

/**
* @Description: 验证是否是声明的几种日志级别
* @date 2017年11月28日 下午5:05:40
* @param @param level
* @param @return
* @return boolean
* @throws
*/
public static boolean contains(LogLevel level){
if(level==null){
return false;
}
for (LogLevel value : LogLevel.values()) {
if(value==level){//有匹配,返回true
return true;
}
}
return false;
}
}
6 changes: 3 additions & 3 deletions src/test/java/com/yun/flogger/test/FloggerTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.yun.flogger.test;

import com.cyfonly.flogger.FLogger;
import com.cyfonly.flogger.constants.Constant;
import com.cyfonly.flogger.constants.Constant.LogLevel;

public class FloggerTest {

Expand All @@ -11,9 +11,9 @@ public static void main(String[] args) {
//简便api,只需指定内容
logger.info("Here is your message...");
//指定日志级别和内容,文件名自动映射
logger.writeLog(Constant.INFO, "Here is your customized level message...");
logger.writeLog(LogLevel.DEBUG, "Here is your customized level message...");
//指定日志输出文件名、日志级别和内容
logger.writeLog("error", Constant.ERROR, "Here is your customized log file and level message...");
logger.writeLog("error", LogLevel.ERROR, "Here is your customized log file and level message...");
}

}