Clean code, clean logs

Many programmers seem to forget how logging application behavior and its current activity is important. When somebody puts:

log.info("!@#$%");


happily somewhere in the code, he probably don't realize the importance of application logs during maintenance, tuning and failure identification. Underestimating the value of good logs is a terrible mistake. I have collected few random advices that I find especially useful when it comes to writing logging routines and I will present them in a series of short articles. First tip (out of ten) is about logging libraries and tools.

In my opinion,  SLF4J is the best logging API available, mostly because of a great pattern substitution support:

log.debug("Found {} records matching filter: '{}'", records, filter);


In Log4j you would have to use:

log.debug("Found " + records + " records matching filter: '" + filter + "'");


This is not only longer and less readable, but also inefficient because of extensive use of string concatenation. SLF4J adds nice {} substitution feature. Also, because string concatenation is avoided and toString() is not called if the logging statement is filtered, there is no need for isDebugEnabled() anymore. BTW, have you noticed  single quotesaround filter string parameter?

SLF4J is just a façade, as an implementation I would recommend  Logback framework, already advertised on my blog, instead of well established  Log4J. It has many interesting features (some of them will be discussed in future tips) and,  in contrary to Log4J, is actively developed.

The last tool to recommend is  Perf4J. To quote their motto:

Perf4J is to System.currentTimeMillis() as log4j is to System.out.println()

I've added Perf4J to one existing application under heavy load and seen it in action in few other. Both administrators and business users were impressed by the nice graphs produced by this simple utility. Also we were able to discover performance flaws in no time. Perf4J itself deserves its own article, but for now just check their  Developer Guide.

To summarize, this is the ideal pom.xml excerpt to start with:

<repositories>
    <repository>
        <id>Version99</id>
        <name>Version 99 Does Not Exist Maven repository</name>
        <layout>default</layout>
        <url>http://no-commons-logging.zapto.org/mvn2</url>
    </repository>
</repositories>


<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.5.11</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>0.9.20</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.5.11</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.5.11</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.5.11</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>99.0-does-not-exist</version>
</dependency>


To test this, try the following code:

SLF4JBridgeHandler.install();

org.apache.log4j.Logger.getLogger("A").info("Log4J");
java.util.logging.Logger.getLogger("B").info("java.util.logging");
org.apache.commons.logging.LogFactory.getLog("C").info("commons-logging");
org.slf4j.LoggerFactory.getLogger("D").info("Logback via SLF4J");


As you can see, no matter which logging framework is used (we don't even have Log4J and  Commons-Logging on our  CLASSPATH, see  99.0-does-not-exist version!), every logging statement is printed using Logback (see  how it works). So even if your favorite libraries stick to Commons-Logging (very bad thing!  [1][2]) or even worse to Log4J, you don't need to include them in your project.


UPDATE:  Ceki Gülcü (founder of the  Log4JSLF4J and  Logback projects) suggested simplier approach to get rid of  commons-logging dependency (see his  comment).


Posted 4th May 2010 by  Tomasz Nurkiewicz
Labels:  log4j  logback  logging  perf4j  slf4j

转载于:https://my.oschina.net/CasparLi/blog/325156

C:\Users\Administrator>npm install opencv4nodejs npm WARN cleanup Failed to remove some directories [ npm WARN cleanup [ npm WARN cleanup 'C:\\Users\\Administrator\\node_modules\\opencv-build', npm WARN cleanup [Error: EBUSY: resource busy or locked, rmdir 'C:\Users\Administrator\node_modules\opencv-build'] { npm WARN cleanup errno: -4082, npm WARN cleanup code: 'EBUSY', npm WARN cleanup syscall: 'rmdir', npm WARN cleanup path: 'C:\\Users\\Administrator\\node_modules\\opencv-build' npm WARN cleanup } npm WARN cleanup ], npm WARN cleanup [ npm WARN cleanup 'C:\\Users\\Administrator\\node_modules', npm WARN cleanup [Error: EBUSY: resource busy or locked, rmdir 'C:\Users\Administrator\node_modules\opencv-build'] { npm WARN cleanup errno: -4082, npm WARN cleanup code: 'EBUSY', npm WARN cleanup syscall: 'rmdir', npm WARN cleanup path: 'C:\\Users\\Administrator\\node_modules\\opencv-build' npm WARN cleanup } npm WARN cleanup ], npm WARN cleanup [ npm WARN cleanup 'C:\\Users\\Administrator\\node_modules\\readable-stream', npm WARN cleanup [Error: EPERM: operation not permitted, rmdir 'C:\Users\Administrator\node_modules\readable-stream'] { npm WARN cleanup errno: -4048, npm WARN cleanup code: 'EPERM', npm WARN cleanup syscall: 'rmdir', npm WARN cleanup path: 'C:\\Users\\Administrator\\node_modules\\readable-stream' npm WARN cleanup } npm WARN cleanup ] npm WARN cleanup ] npm ERR! code 1 npm ERR! path C:\Users\Administrator\node_modules\opencv4nodejs npm ERR! command failed npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node ./install/install.js npm ERR! info install using lib dir: C:/Users/Administrator/node_modules/opencv-build/opencv/build/lib/Release npm ERR! C:\Users\Administrator\node_modules\opencv4nodejs\install\install.js:37 npm ERR! throw new Error('library dir does not exist: ' + libDir) npm ERR! ^ npm ERR! npm ERR! Error: library dir does not exist: C:/Users/Administrator/node_modules/opencv-build/opencv/build/lib/Release npm ERR! at Object.<anonymous> (C:\Users\Administrator\node_modules\opencv4nodejs\install\install.js:37:9) npm ERR! at Module._compile (node:internal/modules/cjs/loader:1254:14) npm ERR! at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) npm ERR! at Module.load (node:internal/modules/cjs/loader:1117:32) npm ERR! at Module._load (node:internal/modules/cjs/loader:958:12) npm ERR! at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) npm ERR! at node:internal/main/run_main_module:23:47 npm ERR! npm ERR! Node.js v18.16.0 npm ERR! A complete log of this run can be found in: npm ERR! H:\nodejs\node_cache\_logs\2023-05-15T14_05_10_717Z-debug-0.log
05-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值