log4j配置(1)

一、log4j的组成:Loggers, Appenders and Layouts
Log4j has three main components: loggers,appenders andlayouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

log4j主要由三部分组成:loggers,appenders,layouts

(1)Loggers

Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule

logger的命名,区分大小写,并且遵循层级命名规则。例如,logger A 名为"com.foo",logger B 名为"com.foo.Bar",那么A 是B的父亲

The root logger resides at the top of the logger hierarchy.Invoking the class staticLogger.getRootLogger method retrieves it. All other loggers are instantiated and retrieved with the class staticLogger.getLogger method.

rootLogger位于最顶层,并且可通过静态方法Logger.getRootLogger获得。

Loggers may be assigned levels. The set of possible levels, that isTRACE,DEBUG,INFO,WARN,ERROR andFATAL are defined in theorg.apache.log4j.Level class.If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level

loggers的记录级别:TRACE,DEBUG,INFO,WARN, ERROR and FATAL 。记录级别具有继承性,例如logger A 未定义记录级别,则会找其父类记录级别

(2)Appenders and Layouts

Log4j allows logging requests to print to multiple destinations. In log4j speak, an output destination is called anappender. Currently, appenders exist for theconsole,files, GUI components,remote socket servers,JMS,NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously.

log4j允许将日志输出 到过个 destination。例如:console,files。。。。


The addAppender method adds an appender to a given logger. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy.

日志输出目的地地 具有 累加性,即日志会输出到logger对象指定的所有输出destination和该logger对象祖先指定的输出destination


log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender -------这句话的意思是定义CONSOLE为org.apache.log4j.ConsoleAppender(log4j的控制台输出),Log4j提供的appender有以下几种:

org.apache.log4j.ConsoleAppender(控制台)

org.apache.log4j.FileAppender(文件)

org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)

org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)

org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)


More often than not, users wish to customize not only the output destination but also the output format. This is accomplished by associating alayout with an appender.

可通过layout指定日志输出的样式


log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout -------定义log4j使用的输出布局(输出格式)。

log4j提供以下4种布局样式:

org.apache.log4j.HTMLLayout(以HTML表格形式布局)

org.apache.log4j.PatternLayout(可以灵活地指定布局模式,就是可以自定义输出样式),

org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),

org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)


log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n -------这个就是针对PatternLayou你自定义的输出格式,重点讲解一下打印参数, Log4J采用的是类似C语言中的printf函数的打印格式格式化日志信息的

%m 输出代码中指定的消息

  %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL

  %r 输出自应用启动到输出该log信息耗费的毫秒数

  %c 输出所属的类目,通常就是所在类的全名

  %t 输出产生该日志事件的线程名

  %n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”,也就是一跳消息占用一行

  %d 输出日志时间点的日期或时间,紧跟一对花括号进行自定义格式

%c 输出所属的类目,通常就是所在类的全名

%l 精确到行

%x 输出对齐


As a side note, let me mention that in log4j child loggers link only to their existing ancestors. In particular, the logger namedcom.foo.Bar is linked directly to theroot logger, thereby circumventing the unusedcom or com.foo loggers. This significantly increases performance and reduces log4j's memory footprint.

child loggers 使用共有的祖先,这样可以明显提高性能



log4j配置实例1:


[html] view plaincopy
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j配置实例2


[html] view plaincopy
log4j.rootLogger=DEBUG, CONSOLE, FILE
## for console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n
## for file
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=D:/logs/log4j.log
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE.Append = true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH\:mm\:ss} [%t] %-5p %c(line-%L) %-4r %x - %m%n
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,你可以使用Log4j来进行日志记录。下面是配置Spring Boot项目中Log4j的步骤: 1. 首先,确保在项目的pom.xml文件中添加了Log4j的依赖。可以在<dependencies>标签内添加以下内容: ```xml <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> ``` 2. 创建一个log4j.properties或log4j.xml文件,并将其放置在src/main/resources目录下。你可以根据自己的需求选择使用properties文件或者xml文件。 3. 在log4j配置文件中,定义日志输出的格式和位置。你可以设置不同的日志级别,例如DEBUG、INFO、WARN、ERROR等。下面是一个简单的log4j.properties配置示例: ```properties # 设置日志级别为INFO log4j.rootLogger=INFO, stdout # 输出到控制台 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n ``` 4. 在Spring Boot应用程序的application.properties或application.yml文件中,添加以下配置,以确保应用程序使用log4j作为日志框架: application.properties: ```properties # 使用log4j作为日志框架 logging.config=classpath:log4j.properties ``` application.yml: ```yaml # 使用log4j作为日志框架 logging: config: classpath:log4j.properties ``` 这样配置后,你就可以在你的代码中使用日志记录了。例如,使用以下代码在类中创建一个Logger对象并使用它进行日志记录: ```java import org.apache.log4j.Logger; public class YourClass { private static final Logger logger = Logger.getLogger(YourClass.class); public void yourMethod() { logger.info("This is an info log message"); logger.error("This is an error log message"); } } ``` 以上就是在Spring Boot中配置Log4j的基本步骤,你可以根据自己的需求进行更高级的配置和定制。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值