自定义日志打印
个人定义风格展示
实现步骤
导入日志相关包,我用maven进行管理, 相关信息如下:
<spring.version>4.2.4.RELEASE</spring.version>
<slf4j.version>1.8.0-alpha0</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.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>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end -->
配置log4j.properties用于配置日志输出格式
log4j.rootLogger=info, ServerDailyRollingFile, stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern=’.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=logs/info.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %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=[%p] %d{yyyy-MM-dd HH:mm:ss} [%c] %m%n
#mybatis log configure
log4j.logger.com.jiang=info
重要: 自定义一个类用于继承ServletContextListener
在继承方法contextInitialized()下添加自己要打印的东西, 我加的内容如下:
public void contextInitialized(ServletContextEvent servletContextEvent) {
log.info(" ......................我佛慈悲......................");
log.info(" _oo0oo_ ");
log.info(" o8888888o ");
log.info(" 88\" . \"88 ");
log.info(" (| -_- |) ");
log.info(" 0\\ = /0 ");
log.info(" ___/‘---’\\___ ");
log.info(" .' \\| |/ '. ");
log.info(" / \\\\||| : |||// \\ ");
log.info(" / _||||| -卍-|||||_ \\ ");
log.info(" | | \\\\\\ - /// | | ");
log.info(" | \\_| ''\\---/'' |_/ | ");
log.info(" \\ .-\\__ '-' ___/-. / ");
log.info(" ___'. .' /--.--\\ '. .'___ ");
log.info" .\"\" ‘< ‘.___\\_<|>_/___.’ >’ \"\". ");
log.info(" | | : ‘- \\‘.;‘\\ _ /’;.’/ - ’ : | | ");
log.info(" \\ \\ ‘_. \\_ __\\ /__ _/ .-’ / / ");
log.info(" =====‘-.____‘.___ \\_____/___.-’___.-’===== ");
log.info(" ‘=---=’ ");
log.info(" ");
log.info("....................佛祖开光 ,永无BUG...................");
}