log4j配置的依賴
<!-- 刪除pom.xml文件中全部對日誌jar包的引用--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </dependency> <!--排除spring boot對[spring-boot-starter-logging]的依賴,並增長依賴包[spring-boot-starter-log4j.jar]--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency>
log4j.properties的配置:html
log4j.rootLogger=info,ServerDailyRollingFile,stdout log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd log4j.appender.ServerDailyRollingFile.File=D://test/test.log log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p [%c] - %m%n log4j.appender.ServerDailyRollingFile.Append=true log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d yyyy-MM-dd HH:mm:ss %p [%c] %m%n
參看:https://www.cnblogs.com/zj0208/p/6149852.htmljava
2:logback 的配置:spring
properties文件:docker
logging.config=classpath:logback-spring.xml
第一種:springboot的logback的默認配置base.xml數據庫
logback-spring.xml:apache
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> //引入springboot默認的日誌配置 <!-- 將日誌寫入數據庫 --> <appender name="DB" class="ch.qos.logback.classic.db.DBAppender"> <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource"> <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource"> <driverClass>oracle.jdbc.driver.OracleDriver</driverClass> <jdbcUrl>jdbc:oracle:thin:@192.168.8.4:1521/orcl</jdbcUrl> <user>admin</user> <password>123456</password> </dataSource> </connectionSource> </appender> <appender name="ASYNC_ROLLING_DB" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="DB"/> </appender> <root level="INFO"> <appender-ref ref="ASYNC_ROLLING_DB"/> </root> </configuration>
第二種:依賴+配置api
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>demo</docker.image.prefix>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- logback用來設置版本號 -->
<slf4j.version>1.7.25</slf4j.version>
<logback.version>1.2.3</logback.version>
</properties>
<!-- logback用到的jar包 --> <!-- logback start --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <!-- logback end -->
logback-spring.xmlspringboot
加打印到控制檯,的以及<appender-ref ref="STDOUT"/>oracle
<?xml version="1.0" encoding="UTF-8"?> <configuration> <--<include resource="org/springframework/boot/logging/logback/base.xml" /> --> //引入springboot默認的日誌配置 <!--打印到控制檯 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <pattern> %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n </pattern> </encoder> </appender> <!-- 將日誌寫入數據庫 --> <appender name="DB" class="ch.qos.logback.classic.db.DBAppender"> <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource"> <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource"> <driverClass>oracle.jdbc.driver.OracleDriver</driverClass> <jdbcUrl>jdbc:oracle:thin:@192.168.8.4:1521/orcl</jdbcUrl> <user>admin</user> <password>123456</password> </dataSource> </connectionSource> </appender> <appender name="ASYNC_ROLLING_DB" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="DB"/> </appender> <root level="INFO"> <appender-ref ref="ASYNC_ROLLING_DB"/>
<appender-ref ref="STDOUT"/>
</root> </configuration>