Skip to main content

日志

本框架在JDK logging的基础上实现了一个日志模块,基本使用方式和SLF4J、Logback一致,但无需外部SLF4J、Logback组件支持。 一共有ALL、TRACE、DEBUG、INFO、WARN、ERROR、OFF七个级别,除去ALL和OFF外,一共五个日志级别。

基本使用#

通常先给需要日志的类添加一个静态日志实例。然后通过该实例在需要日志记录的方法中通过该实例的trace、debug、info、warn、error系列方法输出日志内容。

@RequestMapping(value = "/")
public class LoggerController extends Controller {
private static final Log logger = LogFactory.getLog(LoggerController.class);
@RequestMapping
public Result<String> index() {
// ... logger.trace/debug/info/warn/error
testTrace();
testDebug();
testInfo();
testWarn();
testError();
return Success.of("hello world !");
}
private void testTrace() {
logger.trace("trace message.");
logger.trace("trace message with one parameter x = %s.", 1);
logger.trace("trace message with two parameter y = %s + %s.", 1, 1);
logger.trace(()->{
int i = 0;
int j = 1;
return "trace message with callback Lambda.";
});
try{
throw new RuntimeException();
} catch (RuntimeException e) {
logger.trace(e, "trace message with Exception");
logger.trace(e, "trace message with Exception and two parameter y = %s + %s.", 1, 1);
logger.trace(e, ()->{
int i = 0;
int j = 1;
return "trace message with Exception and callback Lambda.";
});
}
}
private void testDebug() {
logger.debug("debug message.");
logger.debug("debug message with one parameter x = %s.", 1);
logger.debug("debug message with two parameter y = %s + %s.", 1, 1);
logger.debug(()->{
int i = 0;
int j = 1;
return "debug message with callback Lambda.";
});
try{
throw new RuntimeException();
} catch (RuntimeException e) {
logger.debug(e, "debug message with Exception");
logger.debug(e, "debug message with Exception and two parameter y = %s + %s.", 1, 1);
logger.debug(e, ()->{
int i = 0;
int j = 1;
return "debug message with Exception and callback Lambda.";
});
}
}
private void testInfo() {
logger.info("info message.");
logger.info("info message with one parameter x = %s.", 1);
logger.info("info message with two parameter y = %s + %s.", 1, 1);
logger.info(()->{
int i = 0;
int j = 1;
return "info message with callback Lambda.";
});
try{
throw new RuntimeException();
} catch (RuntimeException e) {
logger.info(e, "info message with Exception");
logger.info(e, "info message with Exception and two parameter y = %s + %s.", 1, 1);
logger.info(e, ()->{
int i = 0;
int j = 1;
return "info message with Exception and callback Lambda.";
});
}
}
private void testWarn() {
logger.warn("warn message.");
logger.warn("warn message with one parameter x = %s.", 1);
logger.warn("warn message with two parameter y = %s + %s.", 1, 1);
logger.warn(()->{
int i = 0;
int j = 1;
return "warn message with callback Lambda.";
});
try{
throw new RuntimeException();
} catch (RuntimeException e) {
logger.warn(e, "warn message with Exception");
logger.warn(e, "warn message with Exception and two parameter y = %s + %s.", 1, 1);
logger.warn(e, ()->{
int i = 0;
int j = 1;
return "warn message with Exception and callback Lambda.";
});
}
}
private void testError() {
logger.error("error message.");
logger.error("error message with one parameter x = %s.", 1);
logger.error("error message with two parameter y = %s + %s.", 1, 1);
logger.error(()->{
int i = 0;
int j = 1;
return "error message with callback Lambda.";
});
try{
throw new RuntimeException();
} catch (RuntimeException e) {
logger.error(e, "error message with Exception");
logger.error(e, "error message with Exception and two parameter y = %s + %s.", 1, 1);
logger.error(e, ()->{
int i = 0;
int j = 1;
return "error message with Exception and callback Lambda.";
});
}
}
}

以上代码演示了各种类型日志的输出。

可选设定#

默认情况下,如果系统定位到了任意一个工作空间,日志输出在控制台的同时也会输出到工作空间下日志目录的日志文件中,默认目录为工作空间下的 logs 目录。 控制台和日志文件的输出等级默认是全部日志级别,如果需要设定可以通过修改bootstrap配置文件。由于日志系统必须尽可能早的初始化,不支持通过程序配置。 以下是一些常用日志设置选项:

# configuration for dev environment
---
readyWork:
server:
# This is the default binding address.
ip: 0.0.0.0
# Http port if enableHttp is true.
httpPort: 8080
log:
# 可选级别:ALL、TRACE、DEBUG、INFO、WARN、ERROR、OFF
consoleLogLevel: ALL # 控制台显示的日志级别
fileLogPath: logs/ # 日志文件存储目录,相对于工作空间
separatedFileLog: false # 是否按日志类型分不同文件夹存储日志
fileLogLevel: ALL # 日志存储级别
fileLogCount: 10 # 历史日志文件保留数量
fileLogLimit: 10000000 # 每个日志文件最大存储字节数,超过就会创建新的日志文件
filter:
# 对特定包或类进行日志级别限定,对控制台和文件日志同时有效,但只能小于上面两种总日志级别,大于无效
#work.ready.core.handler.request: INFO

更多日志模块的设定和应用文档将在后续不断完善,重点内容将以专题形式提供。