Logback configuration
官方指导
http://logback.qos.ch/manual/configuration.html
规则
ch.qos.logback.core.joran.JoranConfiguratorBase.java (位于 core)
@Override
protected void addInstanceRules(RuleStore rs) { // is "configuration/variable" referenced in the docs?
rs.addRule(new ElementSelector("configuration/variable"), new PropertyAction());
rs.addRule(new ElementSelector("configuration/property"), new PropertyAction()); rs.addRule(new ElementSelector("configuration/substitutionProperty"), new PropertyAction()); rs.addRule(new ElementSelector("configuration/timestamp"), new TimestampAction());
rs.addRule(new ElementSelector("configuration/shutdownHook"), new ShutdownHookAction());
rs.addRule(new ElementSelector("configuration/define"), new DefinePropertyAction()); // the contextProperty pattern is deprecated. It is undocumented
// and will be dropped in future versions of logback
rs.addRule(new ElementSelector("configuration/contextProperty"), new ContextPropertyAction()); rs.addRule(new ElementSelector("configuration/conversionRule"), new ConversionRuleAction()); rs.addRule(new ElementSelector("configuration/statusListener"), new StatusListenerAction()); rs.addRule(new ElementSelector("configuration/appender"), new AppenderAction<E>());
rs.addRule(new ElementSelector("configuration/appender/appender-ref"), new AppenderRefAction<E>());
rs.addRule(new ElementSelector("configuration/newRule"), new NewRuleAction());
rs.addRule(new ElementSelector("*/param"), new ParamAction(getBeanDescriptionCache()));
}
ch.qos.logback.classic.joran.JoranConfigurator.java (位于classic)
@Override
public void addInstanceRules(RuleStore rs) {
// parent rules already added
super.addInstanceRules(rs); rs.addRule(new ElementSelector("configuration"), new ConfigurationAction()); rs.addRule(new ElementSelector("configuration/contextName"), new ContextNameAction());
rs.addRule(new ElementSelector("configuration/contextListener"), new LoggerContextListenerAction());
rs.addRule(new ElementSelector("configuration/insertFromJNDI"), new InsertFromJNDIAction());
rs.addRule(new ElementSelector("configuration/evaluator"), new EvaluatorAction()); rs.addRule(new ElementSelector("configuration/appender/sift"), new SiftAction());
rs.addRule(new ElementSelector("configuration/appender/sift/*"), new NOPAction()); rs.addRule(new ElementSelector("configuration/logger"), new LoggerAction());
rs.addRule(new ElementSelector("configuration/logger/level"), new LevelAction()); rs.addRule(new ElementSelector("configuration/root"), new RootLoggerAction());
rs.addRule(new ElementSelector("configuration/root/level"), new LevelAction());
rs.addRule(new ElementSelector("configuration/logger/appender-ref"), new AppenderRefAction<ILoggingEvent>());
rs.addRule(new ElementSelector("configuration/root/appender-ref"), new AppenderRefAction<ILoggingEvent>()); // add if-then-else support
rs.addRule(new ElementSelector("*/if"), new IfAction());
rs.addRule(new ElementSelector("*/if/then"), new ThenAction());
rs.addRule(new ElementSelector("*/if/then/*"), new NOPAction());
rs.addRule(new ElementSelector("*/if/else"), new ElseAction());
rs.addRule(new ElementSelector("*/if/else/*"), new NOPAction()); // add jmxConfigurator only if we have JMX available.
// If running under JDK 1.4 (retrotranslateed logback) then we
// might not have JMX.
if (PlatformInfo.hasJMXObjectName()) {
rs.addRule(new ElementSelector("configuration/jmxConfigurator"), new JMXConfiguratorAction());
}
rs.addRule(new ElementSelector("configuration/include"), new IncludeAction()); rs.addRule(new ElementSelector("configuration/consolePlugin"), new ConsolePluginAction()); rs.addRule(new ElementSelector("configuration/receiver"), new ReceiverAction()); }
具体属性规则可进入对应的Action 查看
比如:
public class ConfigurationAction extends Action {
static final String INTERNAL_DEBUG_ATTR = "debug";
static final String PACKAGING_DATA_ATTR = "packagingData";
static final String SCAN_ATTR = "scan";
static final String SCAN_PERIOD_ATTR = "scanPeriod";
static final String DEBUG_SYSTEM_PROPERTY_KEY = "logback.debug";
...
配置文件
1.清单
logback.groovy
logback-test.xml
logback.xml
2.优先级
Logback tries to find a file called logback.groovy in the classpath.
If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath..
If no such file is found, and the executing JVM has the ServiceLoader (JDK 6 and above) the ServiceLoader will be used to resolve an implementation of
com.qos.logback.classic.spi.Configurator
. The first implementation found will be used. See ServiceLoader documentation for more details.If none of the above succeeds, logback configures itself automatically using the
BasicConfigurator
which will cause logging output to be directed to the console.
3.备注
.groovy 是一种基于JVM(Java虚拟机)的敏捷开发语言,文件格式不同于xml
in the classpath 具体是哪里?directly under WEB-INF/classes/ or classes/
第4项什么意思
第5项什么意思?等价于 logback-test.xml
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender> <root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
<configuration
1. debug="true"(配置文件中)
<configuration debug="true">
或者等价方式(java文件中)
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
// print logback's internal status
StatusPrinter.print(lc);
打印如下:
::, |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
::, |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
::, |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
::, |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration.
有时候是这样:
::, |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
::, |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at [file:/E:/e/workspace/simple-logback/target/classes/logback-test.xml]
::, |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
::, |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
::, |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
::, |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
::, |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
::, |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
::, |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@132b73b - Registering current configuration as safe fallback point
2. scan="true"
不指定单位时是毫秒,默认1分钟
<configuration scan="true" scanPeriod="30 seconds" >
修改logback.xml 文件之后,会监听配置文件的改动,30秒一次,如果监听到做了改动,则会使用最新的
变量
1.范围
LOCAL CONTEXT SYSTEM OS
作用于一个配置文件
作用于一个app(整个项目)
作用于多个app(整个JVM)
作用于操作系统
举例:
<property scope="context" name="nodeId" value="firstNode" />
scope允许的值有local、context、system ,默认是local
2.使用
a.引入properties文件,然后${some_properties_key}
Example: Variable file (logback-examples/src/main/java/chapters/configuration/variables1.properties)
USER_HOME=/home/sebastien
引入方式有两种:文件和资源
<configuration>
<property file="src/main/java/chapters/configuration/variables1.properties" />
</configuration>
<property resource="variables1.properties" />
b.定义到配置文件,然后${some_custom_key}
<property scope="context" name="nodeId" value="firstNode" />
c.直接使用${some_context_key}
重要属性
${HOSTNAME} 主机名
${CONTEXT_NAME} 上下文名, 默认值是default,可通过<contextName>yourname</contextName>设置
举例说明
配置发邮件的appender
<contextName>${app.context.name.en}</contextName>
...//省略部分代码
<subject>${CONTEXT_NAME} - ${HOSTNAME}</subject>
收到邮件后就是如下效果
意思是:位于adapp18号机器的mmsi项目发来错误日志邮件。
d.直接使用${some_system_key}
System.getProperties()
常用的有:
file.separator=\
catalina.base=E:\e\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
user.home=C:\Documents and Settings\Administrator
设置日志文件路径时经常用到。
设置拦截级别
Logback configuration的更多相关文章
- 【异常】ERROR in ch.qos.logback.core.joran.spi.Interpreter@159:22 - no applicable action for [charset], current ElementPath is [[configuration][appender][encoder][charset]]
一.异常信息 Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at ...
- logback + slf4j + jboss + spring mvc
logback.log4j.log4j2 全是以同一个人为首的团伙搞出来的(日志专业户!),这几个各有所长,log4j性能相对最差,log4j2性能不错,但是目前跟mybatis有些犯冲(log4j2 ...
- Logback相关知识汇总
例如:%-4relative 表示,将输出从程序启动到创建日志记录的时间 进行左对齐 且最小宽度为4格式修饰符,与转换符共同使用:可选的格式修饰符位于“%”和转换符之间.第一个可选修饰符是左对齐 标志 ...
- 记使用aliyun-log-logback-appender 报错no applicable action for [encoder], current ElementPath is [[configuration][appender][encoder]]
依赖: <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>aliy ...
- logback+slf4j作为日志系统
一.logback简介 log4j和logback作者是同一人:CekiGülcü.log4j和logback都是实打实的日志系统. commons-logging,slf4j这两者是日志大管家.sl ...
- ELK5.X+logback搭建日志平台
一.背景 当生产环境web服务器做了负载集群后,查询每个tomcat下的日志无疑是一件麻烦的事,elk为我们提供了快速查看日志的方法. 二.环境 CentOS7.JDK8.这里使用了ELK5.0.0( ...
- Logback学习笔记
Logback介绍 Logback 分为三个模块:Core.Classic 和 Access.Core模块是其他两个模块的基础. Classic模块扩展了core模块. Classic模块相当于log ...
- SpringBoot使用logback自定义配置时遇到的坑 --- 在 /tmp目录下自动生成spring.log文件
问题描述 SpringBoot项目使用logback自定义配置后,会在/tmp/ 目录下生成 spring.log的文件(如下图所示). 解决方案 通过各种资料的搜索,最终发现问题的所在(logbac ...
- 54. spring boot日志升级篇—logback【从零开始学Spring Boot】
在<44. Spring Boot日志记录SLF4J>章节中有关相关的介绍,这里我们在深入的了解下logback框架. 为什么要使用logback ? --在开发中不建议使用System. ...
随机推荐
- 2017百度之星初赛B-1002(HDU-6115)
一.思路 这题“看似”比较难搞的一点是,一个节点上有多个办公室,这怎么求?其他的,求树中任意两个节点的距离(注意:没有最远或最最进这一说法,因为树上任意两个节点之间有且仅有一条路径.不然就有回路了,对 ...
- HDU5336题解
解题思路 这题思路并不难,主要问题是,不太好编码实现(可能是本人练习不够吧),因为有个时间在里面,而且每个小水滴都同时流动,感觉好复杂的样子.比赛时,我首先想到的是DFS+时间流做参数,由于比赛时神经 ...
- 浅谈PHP面向对象编程(四、类常量和静态成员)
4.0 类常量和静态成员 通过上几篇博客我们了解到,类在实例化对象时,该对象中的成员只被当前对象所有.如果希望在类中定义的成员被所有实例共享. 此时可以使用类常量或静态成员来实现,接下来将针对类常量和 ...
- thinkjs 学习笔记
抽空大概看了下thinkjs,总体感觉很不错 不了解的可以看下文档(http://thinkjs.org/doc.html) 介绍就不多说了,看下快速入门 npm install -g thinkjs ...
- 面试宝典:Java面试中最高频的那20%知识点!
Java目前已经不仅仅是一门开发语言,而是一整套生态体系. 作为一个Java程序员,既是幸运的,也是不幸的.幸运的是我们有很多轮子可以拿过来用,不幸的是我们有太多的轮子需要学习. 但是,无论是日常工作 ...
- find命令中的print0和xargs -0
看到命令find . -name checkout-cache -f -- 不明白其中-print0和 xargs -0的用法.查了一下,转载一篇备忘. xargs命令的作用是将参数列表转换成小块分段 ...
- ThinkPHP自动令牌验证(附实例)
一.数据表结构 user表结构如下: id username password 二.view模板部分 /view/index.html页面如下: 1 2 3 4 5 6 <form acti ...
- update project maven项目的时候出错
preference node "org.eclipse.wst.validation"has been remove 上面的错误是因为修改包名无法互相引入导致的,仅仅需要将Ecl ...
- 【Oracle】Oracle数据库DATABASE LINK 的命名
当前数据库的GLOBAL_NAMES参数设置为 TRUE,使用DATABASE LINK 时,DATABASE LINK的名称必须与被连接库的 GLOBAL_NAME一致. 而要建多个 DBLINK到 ...
- 「小程序JAVA实战」小程序头像图片上传(上)(43)
转自:https://idig8.com/2018/09/08/xiaochengxujavashizhanxiaochengxutouxiangtupianshangchuan40/ 在微信小程序中 ...