博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Logback configuration
阅读量:4965 次
发布时间:2019-06-12

本文共 8433 字,大约阅读时间需要 28 分钟。

官方指导

 

规则

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
()); rs.addRule(new ElementSelector("configuration/appender/appender-ref"), new AppenderRefAction
()); 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
()); rs.addRule(new ElementSelector("configuration/root/appender-ref"), new AppenderRefAction
()); // 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.优先级

  1. Logback tries to find a file called logback.groovy in the classpath.

  2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.

  3. If no such file is found, it checks for the file logback.xml in the classpath..

  4. 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.

  5. If none of the above succeeds, logback configures itself automatically using theBasicConfigurator 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

%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n

<configuration

 

1. debug="true"(配置文件中)

 或者等价方式(java文件中)

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();        // print logback's internal status        StatusPrinter.print(lc);

 

打印如下:

16:24:00,500 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]16:24:00,500 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]16:24:00,500 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]16:24:00,500 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration.

 

有时候是这样:

16:38:19,515 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]16:38:19,515 |-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]16:38:19,625 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]16:38:19,625 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]16:38:19,656 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property16:38:19,687 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG16:38:19,687 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]16:38:19,687 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.16:38:19,687 |-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

引入方式有两种:文件和资源

b.定义到配置文件,然后${some_custom_key}

c.直接使用${some_context_key}

重要属性

${HOSTNAME} 主机名

${CONTEXT_NAME} 上下文名, 默认值是default,可通过<contextName>yourname</contextName>设置

举例说明

配置发邮件的appender

${app.context.name.en}
...//省略部分代码
${CONTEXT_NAME} - ${HOSTNAME}

收到邮件后就是如下效果

意思是:位于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

设置日志文件路径时经常用到。

 设置拦截级别

 

 

转载于:https://www.cnblogs.com/zno2/p/4792215.html

你可能感兴趣的文章
mysql union 组合查询
查看>>
socket tcp
查看>>
spiral-matrix-ii &i 生成顺时针序列
查看>>
python set集合方法总结
查看>>
python考点
查看>>
dstat 监控时,无颜色显示
查看>>
CSS3阴影 box-shadow的使用和技巧总结
查看>>
DataMining--Python基础入门
查看>>
单片机复位电路
查看>>
php json_decode失败,返回null
查看>>
获取单选按钮选中的值
查看>>
oracle 分页
查看>>
助教学期总结
查看>>
绘制基本 图形之矩形与多边形
查看>>
3-day3-list-truple-map.py
查看>>
02: djangorestframework使用
查看>>
7zip 自解压安装程序
查看>>
Graph-tool简介 - wiki
查看>>
jenkins 离线安装插件 ,插件的下载地址
查看>>
Edit控件显示多行文字
查看>>