Skip to content

Commit 04a9c05

Browse files
committed
Create README.md
1 parent 8c81bd2 commit 04a9c05

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
## MinLog
2+
3+
![](https://raw.github.com/wiki/EsotericSoftware/minlog/images/logo.png)
4+
5+
Please use the [MinLog discussion group](http://groups.google.com/group/minlog-users) for support.
6+
7+
## Overview
8+
9+
MinLog is a Java logging library. Key features:
10+
11+
- **Zero overhead** Logging statements below a given level can be automatically removed by javac at compile time. This means applications can have detailed trace and debug logging without having any impact on the finished product.
12+
13+
- **Simple and efficient** The API is concise and the code is very efficient at runtime.
14+
15+
- **Extremely lightweight** The entire project consists of a [single Java file](http://code.google.com/p/minlog/source/browse/trunk/src/com/esotericsoftware/minlog/Log.java) with ~100 non-comment lines of code.
16+
17+
Also see [this](https://github.com/jdanbrown/minlog-slf4j) drop-in replacement for minlog which logs to slf4j.
18+
19+
## Usage
20+
21+
Messages are logged using static methods:
22+
23+
```java
24+
Log.info("Some message.");
25+
Log.debug("Error reading file: " + file, ex);
26+
```
27+
28+
A static import can be used to make the logging more concise:
29+
30+
```java
31+
import static com.esotericsoftware.minlog.Log.*;
32+
// ...
33+
info("Some message.");
34+
debug("Error reading file: " + file, ex);
35+
```
36+
37+
While optional, for brevity the rest of this documentation assumes this static import is in place.
38+
39+
If log statements from different libraries or areas of an application need to be differentiated, a category can be specified as the first argument:
40+
41+
```java
42+
info("some lib", "Some message.");
43+
debug("some lib", "Error reading file: " + file, ex);
44+
```
45+
46+
## Log level
47+
48+
Setting the level will log that level, as well as all higher levels. There are multiple ways to set the current level:
49+
50+
```java
51+
Log.set(LEVEL_INFO);
52+
Log.INFO();
53+
INFO();
54+
```
55+
56+
The levels are:
57+
58+
- NONE disables all logging.
59+
- ERROR is for critical errors. The application may no longer work correctly.
60+
- WARN is for important warnings. The application will continue to work correctly.
61+
- INFO is for informative messages. Typically used for deployment.
62+
- DEBUG is for debug messages. This level is useful during development.
63+
- TRACE is for trace messages. A lot of information is logged, so this level is usually only needed when debugging a problem.
64+
65+
66+
## Conditional logging
67+
68+
If a logging method below the current level is called, it will return without logging the message. In order to avoid string concatenation, the current log level can be checked before the message is logged:
69+
70+
```java
71+
if (ERROR) error("Error reading file: " + file, ex);
72+
if (TRACE) {
73+
StringBuilder builder = new StringBuilder();
74+
// Do work, append to the builder.
75+
trace(builder);
76+
}
77+
```
78+
79+
## Fixed logging levels
80+
81+
MinLog users can choose from the regular "minlog.jar" or from from a JAR file like "minlog-info.jar" which has a fixed logging level that cannot be changed at runtime. When a fixed level JAR is used, code that changes the level will have no affect. During compilation, any conditional logging statements below the fixed level will be automatically removed by the Java compiler. This means they will have absolutely no impact on the application.
82+
83+
## Output customization
84+
85+
The default logger outputs messages in this format:
86+
87+
```
88+
time level: [category] message
89+
```
90+
91+
Where "time" is the time elapsed since the application started. For example:
92+
93+
```
94+
00:00 TRACE: [kryo] Wrote string: moo
95+
00:00 TRACE: [kryo] Wrote object: NonNullTestClass
96+
00:01 TRACE: [kryo] Wrote string: this is some data
97+
00:01 TRACE: [kryo] Compressed to 7.97% using: DeflateCompressor
98+
00:12 TRACE: [kryo] Decompressed using: DeflateCompressor
99+
00:12 TRACE: [kryo] Read string: this is some data
100+
```
101+
102+
The output can be customized:
103+
104+
```java
105+
static public class MyLogger extends Logger {
106+
public void log (int level, String category, String message, Throwable ex) {
107+
StringBuilder builder = new StringBuilder(256);
108+
builder.append(new Date());
109+
builder.append(' ');
110+
builder.append(level);
111+
builder.append('[');
112+
builder.append(category);
113+
builder.append("] ");
114+
builder.append(message);
115+
if (ex != null) {
116+
StringWriter writer = new StringWriter(256);
117+
ex.printStackTrace(new PrintWriter(writer));
118+
builder.append('\n');
119+
builder.append(writer.toString().trim());
120+
}
121+
System.out.println(builder);
122+
}
123+
}
124+
// ...
125+
Log.setLogger(new MyLogger());
126+
```
127+
128+
Using this mechanism, log messages can be filtered (eg, by category), written to a file, etc.

0 commit comments

Comments
 (0)