validating - Logging

本文通过示例代码展示了SLF4J提供的多种日志级别,包括TRACE、DEBUG、INFO、WARN和ERROR,并演示了如何根据不同的严重程度调整日志级别。此外,还介绍了如何程序化地重新加载Logback配置。

SLF4J() provides multiple levels of reporting. Below example shows them all, in increasing order of "seriousness":

// validating/SLF4JLevels.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import org.slf4j.*;

public class SLF4JLevels {
  private static Logger log = LoggerFactory.getLogger(SLF4JLevels.class);

  public static void main(String[] args) {
    log.trace("Hello");
    log.debug("Logging");
    log.info("Using");
    log.warn("the SLF4J");
    log.error("Facade");
    float f = 1.1f + 2.2f;
    System.out.println("f:" + f);
  }
}
/* My Output TRACE level:
2019-04-01T20:50:40.249
[main] TRACE SLF4JLevels - Hello
2019-04-01T20:50:40.251
[main] DEBUG SLF4JLevels - Logging
2019-04-01T20:50:40.251
[main] INFO  SLF4JLevels - Using
2019-04-01T20:50:40.252
[main] WARN  SLF4JLevels - the SLF4J
2019-04-01T20:50:40.252
[main] ERROR SLF4JLevels - Facade
f:3.3000002
*/
/* My Output info level:
2019-04-01T20:52:42.318
[main] INFO  SLF4JLevels - Using
2019-04-01T20:52:42.320
[main] WARN  SLF4JLevels - the SLF4J
2019-04-01T20:52:42.320
[main] ERROR SLF4JLevels - Facade
*/

programmatic reload of logback configurations 

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
configurator.doConfigure(this.getClass().getResourceAsStream("/logback.xml"));

slf4j

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/SLF4JLevels.java

3. https://javachannel.org/posts/programmatic-reload-of-logback-configurations/

4. https://i-blog.csdnimg.cn/blog_migrate/4d3f93aedfdfff372bf2908f5ef876bd.png

################################################################################ # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ################################################################################ # # The properties defined in this file will be made available through system # properties at the very beginning of the Karaf's boot process. # # Log level when the pax-logging service is not available # This level will only be used while the pax-logging service bundle # is not fully available. # To change log levels, please refer to the org.ops4j.pax.logging.cfg file # instead. org.ops4j.pax.logging.DefaultServiceLog.level=ERROR # # Name of this Karaf instance. # karaf.name=root # The default port number to execute karaf. Defaults to 8101 for pdi-server and 8102 for client. karaf.port=8102 # # Default repository where bundles will be loaded from before using # other Maven repositories. For the full Maven configuration, see # the org.ops4j.pax.url.mvn.cfg file. # karaf.default.repository=system # # Location of a shell script that will be run when starting a shell # session. This script can be used to create aliases and define # additional commands. # karaf.shell.init.script=${karaf.home}/etc/shell.init.script # # Sets the maximum size of the shell command history. If not set, # defaults to 500 entries. Setting to 0 will disable history. # # karaf.shell.history.maxSize=0 # # Default role name used for console authorization (JMX, SSH and WEB) # The syntax is the following: # [classname:]principal # where classname is the class name of the principal object # (defaults to org.apache.karaf.jaas.modules.RolePrincipal) # and principal is the name of the principal of that class # (defaults to admin). # # Note that this value can be overriden using the various ConfigAdmin # configurations for JMX, SSH or the WebConsole. # karaf.admin.role=admin # # Set this empty property to avoid errors when validating xml documents. # xml.catalog.files= # # Suppress the bell in the console when hitting backspace too many times # for example # jline.nobell=true # # ServiceMix specs options # org.apache.servicemix.specs.debug=false org.apache.servicemix.specs.timeout=0 # # Settings for the OSGi 4.3 Weaving # By default, we will not weave any classes. Change this setting to include classes # that you application needs to have woven. # org.apache.aries.proxy.weaving.enabled=none # Classes not to weave - Aries default + Xerces which is known to have issues. org.apache.aries.proxy.weaving.disabled=org.objectweb.asm.*,org.slf4j.*,org.apache.log4j.*,javax.*,org.apache.xerces.* # Jetty limits the amount of data that can post back from a browser or other client to the server. # Setting it to 2 million bytes by default. org.eclipse.jetty.server.Request.maxFormContentSize=20000000 # 跳过登录界面设置 KETTLE_DISABLE_LOGIN_SCREEN=Y
最新发布
12-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值