Java日志实战及解析
Java日志实战及解析
日志是程序员必须掌握的基础技能之一,如果您写的软件没有日志,可以说你没有成为一个真正意义上的程序员。
为什么要记日志?
• 监控代码
• 变量变化情况,系统运行过程。
• 产线环境,不太好调试。
• 分布式环境下,调试更困难,日志就是非常好的帮手。
• 统计分析
• 日后审计
• 实际中有4%的代码是日志!
Java日志框架主要有log4j,logback,及其他不常用的官方日志及apachelogging等。
Log4j和LogBack的原作者为同一作者CekiGülcü。主流使用的一般是log4j的居多点,所以本文主要也讲解log4j为主。
配置文件log4j.properites文件,一般放倒classpath目录下即可,无需自启。
|
log4j.rootLogger=debug, stdout,R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%-4r[%t] log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex= log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%-4r[%t] |
log4j.rootLogger=debug, stdout,R
log4j.rootLogger=[Level], Appender1, Appender2
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
其他类似
基本是三大主件的配置:
Logger
日志类型和日志级别(TRACE < DEBUG < INFO < WARN <ERROR < FATAL)
Appenders
输出到哪里,可以有多个目的地( Console,File,GUI 组件,remote socket servers,JMS,NT Event Loggers,remote Unix Syslog daemons。也可以异步)
Appenders的一般Console(ConsoleAppender)用来调试,File有每天更新(DailyRollingFileAppender)和最大多大文件(RollingFileAppender)滚动的设置,也有邮件的告警设置。基本理解上面的即可。
Layouts
%r [%t] %-5p %c - %m%n
176 [main] INFO org.foo.Bar - Located nearest gasstation.
Layouts说明
|
Conversion Character |
Effect |
|
c |
Used to output the category of the logging event. The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed. By default the category name is printed in full. For example, for the category name "a.b.c" the pattern %c{2} will output "b.c". |
|
C |
Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass". WARNING Generating the caller class information is slow. Thus, use should be avoided unless execution speed is not an issue. |
|
d |
Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example,%d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat. For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifyingAbsoluteTimeDateFormat, DateTimeDateFormat and These dedicated date formatters perform significantly better than SimpleDateFormat. |
|
F |
Used to output the file name where the logging request was issued. WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue. |
|
l |
Used to output location information of the caller which generated the logging event. The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses. The location information can be very useful. However, its generation is extremely slow and should be avoided unless execution speed is not an issue. |
|
L |
Used to output the line number from where the logging request was issued. WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue. |
|
m |
Used to output the application supplied message associated with the logging event. |
|
M |
Used to output the method name where the logging request was issued. WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue. |
|
n |
Outputs the platform dependent line separator character or characters. This conversion character offers practically the same performance as using non-portable line separator strings such as "\n", or "\r\n". Thus, it is the preferred way of specifying a line separator. |
|
p |
Used to output the priority of the logging event. |
|
r |
Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event. |
|
t |
Used to output the name of the thread that generated the logging event. |
|
x |
Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event. |
|
X |
Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} where clientNumber is See MDC class for more details. |
|
% |
The sequence %% outputs a single percent sign. |
更多说明:
|
Format modifier |
left justify |
minimum width |
maximum width |
comment |
|
%20c |
false |
20 |
none |
Left pad with spaces if the category name is less than 20 characters long. |
|
%-20c |
true |
20 |
none |
Right pad with spaces if the category name is less than 20 characters long. |
|
%.30c |
NA |
none |
30 |
Truncate from the beginning if the category name is longer than 30 characters. |
|
%20.30c |
false |
20 |
30 |
Left pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning. |
|
%-20.30c |
true |
20 |
30 |
Right pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning. |
Maven引用
|
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.8</version> </dependency> |
个人认为必须了解和掌握的日志特性:
日志级别
继承
JMX管理
Appenders的Additivity
其他的需要了解
Filter
NDC
MDC
日志调优
|
示例: logger.debug(“Entry number: ” + i + “ is ” + String.valueOf(entry[i])); if(logger.isDebugEnabled() { logger.debug(“Entry number: ” + i + “ is ” + String.valueOf(entry[i])); } 判断只是记录日志的1%时间,所以一般情况下是值得的。 但是一般使用SLF4J就自动集成该功能。 |
在了解Log4j之后,也必须了解一下SLF4j,大家一般是使用这个进行整合内部不同日志的具体实现的。
Maven使用
|
• <dependency> • <groupId>org.slf4j</groupId> • <artifactId>slf4j-api</artifactId> • <version>1.7.21</version> • </dependency> |
|
• Logger logger = LoggerFactory.getLogger(HelloWorld.class); • logger.info("Hello World"); |
常用记录日志的地方:
• 方法入口
• 方法出口
• 异常
• 自己需要跟踪的信息
• 距离日志最近的地方记录日志
• 先记录日志,后抛异常,可以把异常往上抛
• 去除冗余日志
日志库的使用情况
日志一般会使用即可。掌握本课程基本够用,剩下来就看你自己了。
著名的solr使用日志的情况
一般大企业,会收集日志,使用ELK等技术查看日志,或sink到hadoop进行数据分析和挖掘,甚至使用storm进行实时统计。
更多内容可以参考视频:
CSDN学 院: http://edu.csdn.net/course/detail/2890
网易云课堂: http://study.163.com/course/introduction/1003149011.htm
Java日志实战及解析的更多相关文章
- Java日志实战及解析 - 引导
日志是什么? 程序日志的作用 记录程序运行情况,程序员观察和调试使用 统计分析 日后审计 程序员,开发工程师,架构师等天天打交道. 实际中有4%的代码是日志! http://logging.apach ...
- 0元免费领《JAVA日志》教程,天啦噜!
天啦,老码疯了!辛辛苦苦,费心费力准备的<java日志实战及解析>教程真的不要钱了吗? 作为添物网的小编,每天看着老码为了给大家录制课程,加班加点的做课件,为了保证课程的质量,老码一遍又一 ...
- [翻译]Java日志终极指南
本文由 ImportNew - Wing 翻译自 loggly.欢迎加入翻译小组.转载请见文末要求. Java日志基础 Java使用了一种自定义的.可扩展的方法来输出日志.虽然Java通过java.u ...
- ETL利器Kettle实战应用解析系列一【Kettle使用介绍】
本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析系列二 [应用场景和实战DEMO下载] 三.ETL利器Kettle ...
- Java日志终极指南
Java日志基础 Java使用了一种自定义的.可扩展的方法来输出日志.虽然Java通过java.util.logging包提供了一套基本的日志处理API,但你可以很轻松的使用一种或者多种其它日志解决方 ...
- MySQL慢日志查询全解析:从参数、配置到分析工具【转】
转自: MySQL慢日志查询全解析:从参数.配置到分析工具 - MySQL - DBAplus社群——围绕数据库.大数据.PaaS云,运维圈最专注围绕“数据”的学习交流和专业社群http://dbap ...
- java 日志体系(四)log4j 源码分析
java 日志体系(四)log4j 源码分析 logback.log4j2.jul 都是在 log4j 的基础上扩展的,其实现的逻辑都差不多,下面以 log4j 为例剖析一下日志框架的基本组件. 一. ...
- Spring Boot系列一:默认日志logback配置解析
前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...
- Java日志 #01# 入门
很多人在学习完一个东西之后就会忘掉自己作为初学者时的体验.. 例如刚接触git的时候自己也是一头雾水,然后别人问起来,老是会说:xxxx#!@#,就是这么回事儿,有什么不好懂的. 其实从不懂到懂,再到 ...
随机推荐
- iOS 锁的常用方法
锁的用法在iOS中有几种方法来解决多线程访问同一个内存地址的互斥同步问题: 方法一,@synchronized(id anObject),(最简单的方法)会自动对参数对象加锁,保证临界区内的代码线程安 ...
- 基于H5+ API手机相册图片压缩上传
// 母函数 function App(){} /** * 图片压缩,默认同比例压缩 * @param {Object} path * pc端传入的路径可以为相对路径,但是在移动端上必须传入的路径是照 ...
- ThreadLocal使用,应用场景,源码实现,内存泄漏
首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的.各 ...
- HTML iframe框架
iframe 作用: 就是在一个网页插入一个小窗口 窗口里面也是一个网页 <a href="http://www.baidu.com" target="da1& ...
- X11/Xlib.h: No such file or directory
CentOS 编译一些开源项目提示:X11/Xlib.h: No such file or directory. 运行命令:yum install libX11-devel就可以了.
- OpenGL 渲染上下文-context
context理解 OpenGL在渲染的时候需要一个Context,这个Context记录了OpenGL渲染需要的所有信息,可以把它理解成一个大的结构体,它里面记录了当前绘制使用的颜色.是否有光照计算 ...
- 测试框架 Mocha 实例教程(转载:来自阮一峰的一篇文章)
Mocha(发音"摩卡")诞生于2011年,是现在最流行的JavaScript测试框架之一,在浏览器和Node环境都可以使用. 所谓"测试框架",就是运行测试的 ...
- false - (失败的)什么都不做
总览 (SYNOPSIS) false [忽略命令行参数] false OPTION 描述 (DESCRIPTION) 程序 结束 时, 产生 表示 失败 的 状态码. 下列的 选项 没有 简写 形式 ...
- null 理解
值 null 特指对象的值未设置.它是 JavaScript 基本类型 之一. 语法节 null 描述节 值 null 是一个字面量,它不像undefined 是全局对象的一个属性.null 是表示缺 ...
- 字符串数组 输入3个字符串,要求按由小到大的字母顺序输出; 输入n个学生的姓名和学号到字符串数组中,在输入一个姓名,如果班级有该生则返回其信息,否则返回本班无此人
输入3个字符串,要求按由小到大的字母顺序输出 如 输入franch england china,输出结果是china england franch 三个数排序输出,比较三个数的大小怎么做? a=18 ...