Log4j maven依赖配置

news/2024/10/30 20:14:59/

做项目的时候,经常需要给应用打印日志,LOG4J是我们的不二选择,项目管理使用maven构建时,pom.xml配置如下

 

       <!--日志 start--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.25</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.25</version><scope>test</scope></dependency><!--日志end-->

 

 

log4j.properties配置样例如下

 

### set log levels ###
log4j.rootLogger=DEBUG### direct log messages to stdout ###
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-2p %m%n### direct messages to file framework.log ###
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
##log4j.appender.A2.File=D:/logs/resmanm.log
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5p(%10c{1}) %m%n### application log config ###
#log4j.logger.com.linkage=ERROR,A2
log4j.logger.com.ch1=DEBUG,A1,A2
##log4j.logger.org.quartz.impl.StdSchedulerFactory=DEBUG,A1,A2

 

最后,附上一段定时器的demo,作为日志打印样例。

 

 

package com.ch1;import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Date;import static org.quartz.DateBuilder.evenMinuteDate;public class SimpleExample {public void run () throws SchedulerException {Logger log = LoggerFactory.getLogger(SimpleExample.class);log.info("------- Initializing ----------------------");SchedulerFactory sf = new StdSchedulerFactory();Scheduler sched = sf.getScheduler();log.info("------- Initialization Complete -----------");// computer a time that is on the next round minuteDate runTime = evenMinuteDate(new Date());log.info("------- Scheduling Job  -------------------");// define the job and tie it to our HelloJob classJobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1","group1").build();// Trigger the job to run on the next round minuteTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group1").startAt(runTime).build();// Start up the scheduler (nothing can actually run until the// scheduler has been started)sched.start();log.info("------- Started Scheduler -----------------");// wait long enough so that the scheduler as an opportunity to// run the job!log.info("------- Waiting 65 seconds... -------------");try {// wait 65 seconds to show jobThread.sleep(65L * 1000L);} catch (InterruptedException e) {e.printStackTrace();}// shut down the schedulerlog.info("------- Shutting Down ---------------------");sched.shutdown(true);log.info("------- Shutdown Complete -----------------");}public static void main(String[] args) throws SchedulerException {SimpleExample example = new SimpleExample();example.run();}
}


开发工具使用的是Idea,日志输出:

 

 

C:\Java\jdk1.8.0_144\bin\java "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.1\lib\idea_rt.jar=56553:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Java\jdk1.8.0_144\jre\lib\rt.jar;E:\cBSS微服务\demo\quartz_test\target\classes;C:\Users\yulei\.m2\repository\org\quartz-scheduler\quartz\2.2.3\quartz-2.2.3.jar;C:\Users\yulei\.m2\repository\c3p0\c3p0\0.9.1.1\c3p0-0.9.1.1.jar;C:\Users\yulei\.m2\repository\org\quartz-scheduler\quartz-jobs\2.2.3\quartz-jobs-2.2.3.jar;C:\Users\yulei\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\yulei\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\yulei\.m2\repository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar com.ch1.SimpleExample
log4j:WARN File option not set for appender [A2].
log4j:WARN Are you using FileAppender instead of ConsoleAppender?
log4j:ERROR Either File or DatePattern options are not set for appender [A2].
log4j:ERROR No output stream or file set for the appender named [A2].
INFO ------- Initializing ----------------------
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
INFO ------- Initialization Complete -----------
INFO ------- Scheduling Job  -------------------
INFO ------- Started Scheduler -----------------
INFO ------- Waiting 65 seconds... -------------
INFO ------- Shutting Down ---------------------
INFO ------- Shutdown Complete -----------------Process finished with exit code 0

 

 

 

 

 

 

 

 

 

 

 

 


http://www.ppmy.cn/news/573886.html

相关文章

ImageJ自动批量荧光面积统计

本方法只介绍批量处理方法&#xff0c;单张图片请再第3步后&#xff0c;点击select&#xff0c;按M即可。 将相类似的需要计算荧光面积的图片复制在同一个文件夹中。 用ImageJ打开其中一张图片&#xff0c; 1&#xff0c;Plugins → Macros → Record&#xff0c;弹出Record…

J1939 入门教程

SAE J1939协议是基于CAN2.0B协议之上的应用层协议&#xff0c;但是SAE J1939协议并不仅仅是个应用层协议&#xff0c;她对物理层&#xff0c;数据链路层&#xff0c;网络层&#xff0c;应用层&#xff0c;故障诊断&#xff0c; 网络层管理层等都做了详细的规定,只不过这其中很多…

经典CNN算法解析实战-第J5周:DenseNet+SE-Net实战

&#x1f368; 本文为&#x1f517;365天深度学习训练营中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊|接辅导、项目定制 目录 一、课题背景和开发环境二、介绍三、SE模块应用分析四、SE模块效果对比五、SE模块代码实现六、SE模块插入到DenseNet代码实现 一、课题背…

深度学习第J5周:DenseNet+SE-Net实战

目录 一、介绍 二、前期准备 二、模型 三、训练运行 3.1训练 3.2指定图片进行预测 &#x1f368; 本文为[&#x1f517;365天深度学习训练营]内部限免文章&#xff08;版权归 *K同学啊* 所有&#xff09; &#x1f356; 作者&#xff1a;[K同学啊] &#x1f4cc; 本周任务&…

数据结构-六度分离(Bloom过滤器)

你听说过“六度分离[1]”实验&#xff0c;并决定亲自测试。你的计划很简单&#xff1a;随机选择两个人&#xff0c;Alice和Bob&#xff0c;以及一个秘密单词。请Alice在他们最喜欢的社交媒体Touhiter上发布该消息&#xff0c;并将其发布给所有追随者&#xff0c;请他们按照相同…

网易游戏(互娱)2021届秋招开始

网易游戏&#xff08;互娱&#xff09;秋招秋招截止至9.25 投递地址: https://game.campus.163.com/home 内推码&#xff1a;NaMukB 更多问题欢迎加qq群1062592666讨论&#xff0c;还有sp-offer内推&#xff5e;hr小姐姐也在的哦&#xff5e;

网易云音乐2020校招

网易云音乐2020校招 http://m.bole.netease.com/#/app/index?boleId67e074d29438c9d0&boleType2&type6&projectId19&signatureebb84bde09e56d114472e68564cbfa5a 点击马上投递&#xff0c;全程分享招聘进度

23届网易秋招内推信息

【网易游戏&#xff08;互娱&#xff09;】2023秋季校园招聘7.26&#xff08;明天&#xff09;即将启动&#xff01; &#x1f449;加入专属答疑社群&#xff1a;QQ【818424638】&#xff08;或扫描群二维码&#xff09; 即可获取网易专属&#x1f381;求职资料大礼包&#x1f…