💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 创建一个java工程 ![](https://box.kancloud.cn/f4ea78af2b5c758c55cdfedf10b48d32_514x838.png) **在项目的根目录下创建** ~~~ ### 设置### #设置logger级别DEBUG、INFO、WRNING、ERROR和输出格式A、B、C或D log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### 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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n ### 输出DEBUG 级别以上的日志到=/Users/jdxia/Desktop/website/log4j/log.log ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = /Users/jdxia/Desktop/website/log4j/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 输出ERROR 级别以上的日志到=/Users/jdxia/Desktop/website/log4j/error.log ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =/Users/jdxia/Desktop/website/log4j/error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ~~~ **设置日志内容** ~~~ package com.jdxia; import org.apache.log4j.Logger; public class Test { //获取日志记录器Logger,名字为本类类名 private static Logger log = Logger.getLogger(Test.class); public static void main(String[] args) { //记录debug级别的日志 log.debug("this is debug message"); //记录info级别的日志 log.info("this is info message"); //记录error级别的日志 log.error("this is error message"); } } ~~~ **输出结果** 1. 首先是控制台的信息 ![](https://box.kancloud.cn/0e3c0b69c57306c1d2865824e3b7d989_1368x344.png) 2. 再来看输出文件 ![](https://box.kancloud.cn/57523180b944dbee0c57d35fac11278a_115x52.png) 发现已按照要求输出到对应的文档中去了