荒淫来到狗窝

logback日志分类收集配置

原文链接地址:logback日志分类收集配置

 

第一步:用logback修改log日志输出。

Maven中引入Jar包 

 1 <!-- logback start -->
 2 
 3 <dependency>
 4 
 5 <groupId>ch.qos.logback</groupId>
 6 
 7 <artifactId>logback-core</artifactId>
 8 
 9 <version>1.0.13</version>
10 
11 </dependency>
12 
13 <dependency>
14 
15 <groupId>ch.qos.logback</groupId>
16 
17 <artifactId>logback-classic</artifactId>
18 
19 <version>1.0.13</version>
20 
21 </dependency>
22 
23 <dependency>
24 
25 <groupId>org.slf4j</groupId>
26 
27 <artifactId>slf4j-api</artifactId>
28 
29 <version>1.7.5</version>
30 
31 </dependency>
32 
33 <dependency>
34 
35 <groupId>org.slf4j</groupId>
36 
37 <artifactId>jcl-over-slf4j</artifactId>
38 
39 <version>1.7.5</version>
40 
41 </dependency>
42 
43 <dependency>
44 
45 <groupId>org.slf4j</groupId>
46 
47 <artifactId>log4j-over-slf4j</artifactId>
48 
49 <version>1.7.5</version>
50 
51 </dependency> 

 

第二步:配置logback.xml至项目目录

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <configuration>
  4 
  5     <!-- stdout appender定义 -->
  6 
  7     <appender name="STDOUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
  8 
  9     
 10 
 11         <File>${HOME}/logs/blog/stdout.log</File>
 12 
 13         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 14 
 15             <!-- rollover daily -->
 16 
 17             <fileNamePattern>${HOME}/logs/blog/stdout.log.%d</fileNamePattern>
 18 
 19             <maxHistory>90</maxHistory>
 20 
 21             <cleanHistoryOnStart>true</cleanHistoryOnStart>
 22 
 23         </rollingPolicy>
 24 
 25         <encoder>
 26 
 27             <charset>UTF-8</charset>
 28 
 29             <pattern>%d %-5level [%thread][%X{hostName}][%X{userId}][%X{sessionId}] %logger{68} %line - %msg%n</pattern>
 30 
 31         </encoder>
 32 
 33     </appender>
 34 
 35     <appender name="DEBUGOUT" class="ch.qos.logback.core.ConsoleAppender">
 36 
 37         <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
 38 
 39             <level>DEBUG</level>
 40 
 41         </filter>
 42 
 43         <encoder>
 44 
 45             <charset>UTF-8</charset>
 46 
 47             <pattern>%d %-5level [%thread][%X{hostName}][%X{userId}][%X{sessionId}] %logger{68} %line - %msg%n</pattern>
 48 
 49         </encoder>
 50 
 51     </appender>
 52 
 53  
 54 
 55     <!-- 全局日志文件 appender定义 -->
 56 
 57     <appender name="lbcAppender"
 58 
 59               class="ch.qos.logback.core.rolling.RollingFileAppender">
 60 
 61  
 62 
 63         <File>${HOME}/logs/blog/app.log</File>
 64 
 65         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 66 
 67             <!-- rollover daily -->
 68 
 69             <fileNamePattern>${HOME}/logs/blog/app.log.%d</fileNamePattern>
 70 
 71             <maxHistory>90</maxHistory>
 72 
 73             <cleanHistoryOnStart>true</cleanHistoryOnStart>
 74 
 75         </rollingPolicy>
 76 
 77  
 78 
 79         <encoder>
 80 
 81             <charset>UTF-8</charset>
 82 
 83             <pattern>%d %-5level [%thread][%X{hostName}][%X{userId}][%X{sessionId}] %logger{36} %line - %msg%n</pattern>
 84 
 85         </encoder>
 86 
 87     </appender>
 88 
 89  
 90 
 91     <!-- sql 日志 appender定义 -->
 92 
 93     <appender name="sqlAppender"
 94 
 95               class="ch.qos.logback.core.rolling.RollingFileAppender">
 96 
 97         <File>${HOME}/logs/blog/sql.log</File>
 98 
 99         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
100 
101             <!-- rollover daily -->
102 
103             <fileNamePattern>${HOME}/logs/blog/sql.log.%d
104 
105             </fileNamePattern>
106 
107             <maxHistory>90</maxHistory>
108 
109             <cleanHistoryOnStart>true</cleanHistoryOnStart>
110 
111         </rollingPolicy>
112 
113         <encoder>
114 
115             <charset>UTF-8</charset>
116 
117             <pattern>%d %-5level [%thread][%X{hostName}][%X{userId}][%X{sessionId}] %logger{68} %line - %msg%n</pattern>
118 
119         </encoder>
120 
121     </appender>
122 
123     
124 
125     <!-- 服务调用service日志 -->
126 
127     <appender name="serviceAppender"
128 
129               class="ch.qos.logback.core.rolling.RollingFileAppender">
130 
131         <File>${HOME}/logs/blog/service.log</File>
132 
133         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
134 
135             <!-- rollover daily -->
136 
137             <fileNamePattern>${HOME}/logs/blog/service.log.%d
138 
139             </fileNamePattern>
140 
141             <maxHistory>90</maxHistory>
142 
143             <cleanHistoryOnStart>true</cleanHistoryOnStart>
144 
145         </rollingPolicy>
146 
147         <encoder>
148 
149             <charset>UTF-8</charset>
150 
151             <pattern>%d %-5level [%thread][%X{hostName}][%X{userId}][%X{sessionId}] %logger{36} %line - %msg%n</pattern>
152 
153         </encoder>
154 
155     </appender>
156 
157     
158 
159     <!-- 请求request日志 -->
160 
161     <appender name="requestAppender"
162 
163               class="ch.qos.logback.core.rolling.RollingFileAppender">
164 
165         <File>${HOME}/logs/blog/request.log</File>
166 
167         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
168 
169             <!-- rollover daily -->
170 
171             <fileNamePattern>${HOME}/logs/blog/request.log.%d
172 
173             </fileNamePattern>
174 
175             <maxHistory>90</maxHistory>
176 
177             <cleanHistoryOnStart>true</cleanHistoryOnStart>
178 
179         </rollingPolicy>
180 
181         <encoder>
182 
183             <charset>UTF-8</charset>
184 
185             <pattern>%d %-5level [%thread][%X{hostName}][%X{userId}][%X{sessionId}] %logger{36} %line - %msg%n</pattern>
186 
187         </encoder>
188 
189     </appender>
190 
191  
192 
193 <!-- error日志 -->
194 
195     <appender name="lbcErrorAppender"
196 
197               class="ch.qos.logback.core.rolling.RollingFileAppender">
198 
199         <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
200 
201             <level>ERROR</level>
202 
203         </filter>
204 
205  
206 
207         <File>${HOME}/logs/blog/error.log</File>
208 
209         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
210 
211             <FileNamePattern>${HOME}/logs/blog/error.log.%d
212 
213             </FileNamePattern>
214 
215             <maxHistory>90</maxHistory>
216 
217             <cleanHistoryOnStart>true</cleanHistoryOnStart>
218 
219         </rollingPolicy>
220 
221         <encoder>
222 
223             <charset>UTF-8</charset>
224 
225             <Pattern>%d %-5level [%thread][%X{hostName}][%X{userId}][%X{sessionId}] %logger{68} %line - %msg%n</Pattern>
226 
227         </encoder>
228 
229     </appender>
230 
231  
232 
233 <!-- 应用程序日志 -->
234 
235     <logger name="com.zblog" additivity="false">
236 
237         <level value="INFO" />
238 
239         <appender-ref ref="lbcAppender" />
240 
241         <appender-ref ref="lbcErrorAppender" />
242 
243     </logger>
244 
245  
246 
247 <!-- 数据库日志拦截  -->
248 
249     <logger name="com.zblog.util.LbcMybatisInterceptor" additivity="false">
250 
251         <level value="INFO" />
252 
253         <appender-ref ref="sqlAppender" />
254 
255     </logger>
256 
257   
258 
259    <!-- API 服务调用日志拦截  -->
260 
261     <logger name="com.zblog.api" additivity="false">
262 
263         <level value="INFO" />
264 
265         <appender-ref ref="serviceAppender" />
266 
267         <appender-ref ref="lbcErrorAppender" />
268 
269     </logger>
270 
271     
272 
273     <!-- Dubbo 服务调用日志拦截 --> 
274 
275     <logger name="com.zblog.common.filter.DubboInvokeFilter" additivity="false">
276 
277         <level value="INFO" />
278 
279         <appender-ref ref="serviceAppender" />
280 
281         <appender-ref ref="lbcErrorAppender" />
282 
283     </logger>
284 
285     
286 
287     
288 
289     <!-- 网站访问请求日志-->
290 
291     <logger name="com.zblog.web.interceptor.IfaexLogMdcIntercepter" additivity="false">
292 
293         <level value="INFO" />
294 
295         <appender-ref ref="requestAppender" />
296 
297     </logger>
298 
299  
300 
301     <root level="INFO">
302 
303         <appender-ref ref="lbcAppender" />
304 
305     </root>
306 
307 </configuration>

 

 

其中配置文件中的{HOME}可以设置为环境变量,用于linux/Window的日志集中收集。

 

启动程序后,在对应的HOME环境目录下能看到日志打印即可。

posted @ 2016-08-09 00:39 月·漩涡 阅读(...) 评论(...) 编辑 收藏

荒淫来到狗窝