方式一
依赖:
<!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end -->
输出配置:/src/main/resources/log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=d:\\mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### ### fatal error warn info debug trace log4j.rootLogger=info, stdout
或者:(推荐)参考:https://www.cnblogs.com/xiaobaizhiqian/p/7956690.html
### 配置根 ### log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE ### 设置输出sql的级别,其中logger后面的内容全部为jar包中所包含的包名 ### log4j.logger.org.apache=dubug log4j.logger.java.sql.Connection=dubug log4j.logger.java.sql.Statement=dubug log4j.logger.java.sql.PreparedStatement=dubug log4j.logger.java.sql.ResultSet=dubug ### 配置输出到控制台 ### log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n ### 配置输出到文件 ### log4j.appender.fileAppender = org.apache.log4j.FileAppender log4j.appender.fileAppender.File = logs/log.log log4j.appender.fileAppender.Append = true log4j.appender.fileAppender.Threshold = DEBUG log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout log4j.appender.fileAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 配置输出到文件,并且每天都创建一个文件 ### log4j.appender.dailyRollingFile = org.apache.log4j.DailyRollingFileAppender log4j.appender.dailyRollingFile.File = logs/log.log log4j.appender.dailyRollingFile.Append = true log4j.appender.dailyRollingFile.Threshold = DEBUG log4j.appender.dailyRollingFile.layout = org.apache.log4j.PatternLayout log4j.appender.dailyRollingFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 配置输出到文件,且大小到达指定尺寸的时候产生一个新的文件 ### log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold=ERROR log4j.appender.ROLLING_FILE.File=rolling.log log4j.appender.ROLLING_FILE.Append=true log4j.appender.ROLLING_FILE.MaxFileSize=10KB log4j.appender.ROLLING_FILE.MaxBackupIndex=1 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ### 配置输出到邮件 ### log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Threshold=FATAL log4j.appender.MAIL.BufferSize=10 log4j.appender.MAIL.From=chenyl@yeqiangwei.com log4j.appender.MAIL.SMTPHost=mail.hollycrm.com log4j.appender.MAIL.Subject=Log4J Message log4j.appender.MAIL.To=chenyl@yeqiangwei.com log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ### 配置输出到数据库 ### log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver log4j.appender.DATABASE.user=root log4j.appender.DATABASE.password= log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n') log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=SampleMessages.log4j log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout
aplication.properties中:
logging.path=/tmp/log
logging.level.org.hibernate.SQL=DEBUG
server.tomcat.basedir=${logging.path}/tomcat
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <!--Sea 整理于 2018-08-31 --> <configuration debug="false" scan="true" scanPeriod="30 seconds"> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <jmxConfigurator/> <springProperty scope="context" name="springAppName" source="spring.application.name" defaultValue="application"/> <springProperty scope="context" name="logPath" source="logging.path" defaultValue="/tmp/log"/> <property name="LOG_FILE" value="${logPath}/application/milestone-subscription-service"/> <property name="SPRING_FILE" value="${logPath}/spring/spring"/> <property name="KAFKA_FILE" value="${logPath}/kafka/kafka"/> <property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/> <!-- console Appender --> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <!--<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter>--> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> </appender> <!--spring Appender--> <appender name="spring" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${SPRING_FILE}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!-- daily rollover --> <fileNamePattern>${SPRING_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxHistory>7</maxHistory> <maxFileSize>100MB</maxFileSize> <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> </appender> <!-- use logstash generate json format log Appender --> <appender name="logstash" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}.log.%d{yyyy-MM-dd}.%i.gz</fileNamePattern> <maxHistory>7</maxHistory> <maxFileSize>100MB</maxFileSize> <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> <!--<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"> <providers> <timestamp> <timeZone>UTC</timeZone> </timestamp> <pattern> <pattern> { "severity": "%level", "service": "${springAppName:-}", "trace": "%X{X-B3-TraceId:-}", "span": "%X{X-B3-SpanId:-}", "exportable": "%X{X-Span-Export:-}", "pid": "${PID:-}", "thread": "%thread", "class": "%logger{40}", "rest": "%message" } </pattern> </pattern> </providers> </encoder>--> </appender> <appender name="kafka" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${KAFKA_FILE}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${KAFKA_FILE}.log.%d{yyyy-MM-dd}.%i.gz</fileNamePattern> <maxHistory>7</maxHistory> <maxFileSize>100MB</maxFileSize> <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> </appender> <!--set the xxl job thread log level to warn--> <logger name="com.xxl.job.core.thread" level="WARN"/> <logger name="org.springframework.retry.support.RetryTemplate" level="debug" /> <logger name="com.icil.elsa.subscribe.milestone.listener" level="DEBUG" additivity="false"> <appender-ref ref="kafka" /> </logger> <logger name="org.apache.kafka.clients.consumer.internals" level="INFO" additivity="false"> <appender-ref ref="kafka" /> </logger> <logger name="org.springframework.kafka.listener" level="INFO" additivity="false"> <appender-ref ref="kafka" /> </logger> <root level="INFO"> <appender-ref ref="console"/> <appender-ref ref="spring"/> </root> <logger name="com.icil.tracking.milestone.service" level="INFO" additivity="false"> <appender-ref ref="console"/> <appender-ref ref="spring"/> <appender-ref ref="logstash"/> </logger> <logger name="org.hibernate.engine.internal.StatisticalLoggingSessionEventListener" level="ERROR" additivity="false"> <appender-ref ref="console"/> <appender-ref ref="spring"/> <appender-ref ref="logstash"/> </logger> </configuration>