Linux 日誌打印到文件的2種方式

1. 打印日誌到httx/logs/out.log中,日誌一直累加,需要跑定時任務半夜1點切割日誌。 如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" monitorInterval="30">
	<Properties>
		<Property name="log_path">/httx/logs</Property>
	</Properties>
	<!-- appender配置 -->
	<Appenders>
		<File name="LogOut" fileName="${log_path}/out.log"
			  filePermissions="rw-r--r--">
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t %c.%M - %m%n" />
		</File>
	</Appenders>
	<!-- logger配置 -->
	<Loggers>
		<Root level="info">
			<AppenderRef ref="LogOut" />
		</Root>
	</Loggers>
</Configuration>

2. 打印日誌到httx/logs/out.log中,日誌按照天寫入文件中。 如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" monitorInterval="30">

<Properties>
	<Property name="log_path">/httx/logs</Property>
</Properties>

<!-- appender配置 -->
<Appenders>

	<Console name="Console" target="SYSTEM_OUT">
		<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%t] %c.%M - %m%n"/>
	</Console>

	<!--DEBUG級別日誌-->
	<RollingRandomAccessFile name="Debug" fileName="${log_path}/debug.log"
							 filePattern="${log_path}/debug.log%d{yyyy-MM-dd}-%i">
		<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%t %l] %c.%M - %m%n"/>
		<Policies>
			<TimeBasedTriggeringPolicy/>
			<SizeBasedTriggeringPolicy size="300MB"/>
		</Policies>
	</RollingRandomAccessFile>

	<!--INFO級別日誌-->
	<RollingRandomAccessFile name="Info" fileName="${log_path}/info.log"
							 filePattern="${log_path}/debug.log%d{yyyy-MM-dd}-%i">
		<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%t %l] %c.%M - %m%n"/>
		<Filters>
			<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
			<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
		</Filters>
		<Policies>
			<TimeBasedTriggeringPolicy/>
			<SizeBasedTriggeringPolicy size="300MB"/>
		</Policies>
	</RollingRandomAccessFile>
	<!-- ERORR級別日誌 -->

	<RollingRandomAccessFile name="Error" fileName="${log_path}/error.log"
							 filePattern="${log_path}/warn.log%d{yyyy-MM-dd}-%i">
		<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%t %l] %c.%M - %m%n"/>
		<Policies>
			<TimeBasedTriggeringPolicy/>
			<SizeBasedTriggeringPolicy size="300MB"/>
		</Policies>
		<Filters>
			<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
		</Filters>
	</RollingRandomAccessFile>

	<!--配置日誌寫到文件-->
	<RollingRandomAccessFile name="out" fileName="${log_path}/out.log"
							 filePattern="${log_path}/out.log%d{yyyy-MM-dd}-%i">
		<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %t %l %c.%M - %m%n"/>
		<Policies>
			<TimeBasedTriggeringPolicy/>
			<SizeBasedTriggeringPolicy size="300MB"/>
		</Policies>
	</RollingRandomAccessFile>

	<Async name="Async">
		<AppenderRef ref="Console"/>
		<AppenderRef ref="Info"/>
		<AppenderRef ref="Error"/>
		<AppenderRef ref="out"/>
	</Async>

</Appenders>

<Loggers>
	<Root level="info">
		<AppenderRef ref="Console"/>
		<AppenderRef ref="Info"/>
		<AppenderRef ref="Error"/>
		<AppenderRef ref="out"/>
		<!--異步配置-->
		<!--<AppenderRef ref="Async"/>-->
	</Root>
</Loggers>

</Configuration>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章