如何通过配置log4j.xml来打印指定类或指定级别的日志信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过配置log4j.xml来打印指定类或指定级别的日志信息相关的知识,希望对你有一定的参考价值。

参考技术A 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- appender 部分是可用的日志输出方式定义,可以定义多个 -->
<appender name="STDOUT" cla<a href="https://www.baidu.com/s?wd=ss&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydryf3P17buWmzryfznvRd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHbzPHckP10LnWTzPjn4njmvPs" target="_blank" class="baidu-highlight">ss</a>="org.apache.log4j.ConsoleAppender">
<layout cla<a href="https://www.baidu.com/s?wd=ss&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydryf3P17buWmzryfznvRd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHbzPHckP10LnWTzPjn4njmvPs" target="_blank" class="baidu-highlight">ss</a>="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %dISO8601-- %p -- %m%n"/>
</layout>
</appender>
<appender name="<a href="https://www.baidu.com/s?wd=DEBUG&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydryf3P17buWmzryfznvRd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHbzPHckP10LnWTzPjn4njmvPs" target="_blank" class="baidu-highlight">DEBUG</a>" cla<a href="https://www.baidu.com/s?wd=ss&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydryf3P17buWmzryfznvRd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHbzPHckP10LnWTzPjn4njmvPs" target="_blank" class="baidu-highlight">ss</a>="org.apache.log4j.RollingFileAppender">
<param name="File" value="e:/logs/<a href="https://www.baidu.com/s?wd=debug&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydryf3P17buWmzryfznvRd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHbzPHckP10LnWTzPjn4njmvPs" target="_blank" class="baidu-highlight">debug</a>.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %dISO8601-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="<a href="https://www.baidu.com/s?wd=DEBUG&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydryf3P17buWmzryfznvRd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHbzPHckP10LnWTzPjn4njmvPs" target="_blank" class="baidu-highlight">DEBUG</a>" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="e:/logs/info.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %dISO8601-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="e:/logs/warn.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %dISO8601-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="e:/logs/error.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %dISO8601-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>

<appender name="WATCHTASK" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="e:/logs/watchtask.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %dISO8601-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>

<!-- category 部分定义了特定包或者类的输出级别和方式,可以有多个 -->
<category name="org.apache">
<priority value="warn"/>
<appender-ref ref="WARN"/>
</category>

<!-- 打印日志到固定的文件中WATCHTASK 选择自定义的日志级别设置 -->
<category name="com.test.task.WatchTimerTask">
<priority value="info"/>
<appender-ref ref="WATCHTASK"/>
</category>

<!-- root部分定义了log4j的默认输出级别和方式 -->
<root>
<priority value="warn"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="STDOUT"/>

</root>
</log4j:configuration>
(1). 输出方式appender一般有5种:org.apache.log4j.RollingFileAppender(滚动文件,自动记录最新日志)org.apache.log4j.ConsoleAppender (控制台)??org.apache.log4j.FileAppender (文件)org.apache.log4j.DailyRollingFileAppender (每天产生一个日志文件)org.apache.log4j.WriterAppender (将日志信息以流格式发送到任意指定的地方)(2). 日记记录的优先级priority,优先级由高到低分为 OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。(3). 格式说明layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出):%c 输出所属类的全名%d 输出日志时间其格式为 %dyyyy-MM-dd HH:mm:ss,SSS,可指定格式 如 %dHH:mm:ss%l? 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数%n 换行符%m 输出代码指定信息,如info(“message”),输出message%p 输出优先级,即 FATAL ,ERROR 等%r 输出从启动到显示该log信息所耗费的<a href="https://www.baidu.com/s?wd=%E6%AF%AB%E7%A7%92&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydryf3P17buWmzryfznvRd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHbzPHckP10LnWTzPjn4njmvPs" target="_blank" class="baidu-highlight">毫秒</a>数%t 输出产生该日志事件的线程名本回答被提问者采纳

delphi如何打印到指定的打印机

参考技术A 一、设置默认打印机,并在相关代码中,将指定打印机设置为-1;
procedure SetPaperHeight(Value:integer); //设置纸张高度-单位:mmvar Device : array[0..255] of char; Driver : array[0..255] of char; Port : array[0..255] of char; hDMode : THandle; PDMode : PDEVMODE;begin if Value < 90 then Value := 90; //自定义纸张最小高度127mm if Value > 432 then Value := 432; //自定义纸张最大高度432mm Printer.PrinterIndex := Printer.PrinterIndex; Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERLENGTH; pDMode^.dmPaperSize := DMPAPER_USER; pDMode^.dmPaperLength := Value * 10; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex;end;
procedure SetPaperWidth(Value:integer); //设置纸张宽度:单位--mmvar Device : array[0..255] of char; Driver : array[0..255] of char; Port : array[0..255] of char; hDMode : THandle; PDMode : PDEVMODE;begin if Value < 100 then Value := 100; //自定义纸张最小宽度76mm if Value > 216 then Value := 216; //自定义纸张最大宽度216mm Printer.PrinterIndex := Printer.PrinterIndex; Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERWIDTH; pDMode^.dmPaperSize := DMPAPER_USER; pDMode^.dmPaperWidth := Value * 10; //将毫米单位转换为0.1mm单位 pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex;
Printer.PrinterIndex :=-1;如果设置为为-1,则为系统默认打印机;
end;
二、或者使用打印对话框,代码通过读取用户选择的打印机,进行指定打印机操作。
procedure Tfr_mai1.Button1Click(Sender: TObject);begin PrintDialog1.PrintToFile:=true; PrintDialog1.PrintRange:= prPageNums;
PrintDialog1.Execute;
end;
单击本按钮将会出现一个打印机选择对话框,可供用户选择指定的打印机.本回答被提问者和网友采纳

以上是关于如何通过配置log4j.xml来打印指定类或指定级别的日志信息的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 打印sql log配置

背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口

如何写一个log4j.xml的工具类方法

关于log4j日志级别问题,懂的来

tomcat启动后,总是输出这些日志,想了解一下是啥意思

log4j.xml打印mybatis sql