• Stars
    star
    172
  • Rank 221,201 (Top 5 %)
  • Language
    Java
  • License
    BSD 3-Clause "New...
  • Created almost 11 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimal overhead Java logging

MinLog

Please use the MinLog discussion group for support.

Overview

MinLog is a tiny Java logging library which features:

  • 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.

  • Extremely lightweight The entire project consists of a single Java file with ~100 non-comment lines of code.

  • Simple and efficient The API is concise and the code is very efficient at runtime.

Also see this drop-in replacement for minlog which logs to slf4j.

Usage

Messages are logged using static methods:

    Log.info("Some message.");
    Log.debug("Error reading file: " + file, ex);

A static import can be used to make the logging more concise:

    import static com.esotericsoftware.minlog.Log.*;
    // ...
    info("Some message.");
    debug("Error reading file: " + file, ex);

While optional, for brevity the rest of this documentation assumes this static import is in place.

If log statements from different libraries or areas of an application need to be differentiated, a category can be specified as the first argument:

    info("some lib", "Some message.");
    debug("some lib", "Error reading file: " + file, ex);

Log level

Setting the level will log that level, as well as all higher levels. There are multiple ways to set the current level:

    Log.set(LEVEL_INFO);
    Log.INFO();
    INFO();

The levels are:

  • NONE disables all logging.
  • ERROR is for critical errors. The application may no longer work correctly.
  • WARN is for important warnings. The application will continue to work correctly.
  • INFO is for informative messages. Typically used for deployment.
  • DEBUG is for debug messages. This level is useful during development.
  • TRACE is for trace messages. A lot of information is logged, so this level is usually only needed when debugging a problem.

Conditional logging

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:

    if (ERROR) error("Error reading file: " + file, ex);
    if (TRACE) {
    	StringBuilder builder = new StringBuilder();
    	// Do work, append to the builder.
    	trace(builder);
    }

Fixed logging levels

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.

Output customization

The default logger outputs messages in this format:

    time level: [category] message

Where "time" is the time elapsed since the application started. For example:

    00:00 TRACE: [kryo] Wrote string: moo
    00:00 TRACE: [kryo] Wrote object: NonNullTestClass
    00:01 TRACE: [kryo] Wrote string: this is some data
    00:01 TRACE: [kryo] Compressed to 7.97% using: DeflateCompressor
    00:12 TRACE: [kryo] Decompressed using: DeflateCompressor
    00:12 TRACE: [kryo] Read string: this is some data

The output can be customized:

    static public class MyLogger extends Logger {
    	public void log (int level, String category, String message, Throwable ex) {
    		StringBuilder builder = new StringBuilder(256);
    		builder.append(new Date());
    		builder.append(' ');
    		builder.append(level);
    		builder.append('[');
    		builder.append(category);
    		builder.append("] ");
    		builder.append(message);
    		if (ex != null) {
    			StringWriter writer = new StringWriter(256);
    			ex.printStackTrace(new PrintWriter(writer));
    			builder.append('\n');
    			builder.append(writer.toString().trim());
    		}
    		System.out.println(builder);
    	}
    }
    // ...
    Log.setLogger(new MyLogger());

Using this mechanism, log messages can be filtered (eg, by category), written to a file, etc.

More Repositories

1

kryo

Java binary serialization and cloning: fast, efficient, automatic
HTML
6,187
star
2

spine-runtimes

2D skeletal animation runtimes for Spine.
C++
4,375
star
3

kryonet

TCP/UDP client/server library for Java, based on Kryo
Java
1,818
star
4

reflectasm

High performance Java reflection
Java
1,525
star
5

yamlbeans

Java object graphs, to and from YAML automatically
Java
561
star
6

spine-scripts

Scripts to export from PhotoShop and other tools to Spine's JSON data format.
JavaScript
363
star
7

tablelayout

Table-based layout for Java UI toolkits: libgdx, Swing, Android, TWL
Java
281
star
8

spine-superspineboy

Example platformer game for spine-libgdx.
Java
224
star
9

clippy

Multifunctional Windows productivity tool for programmers and other power users
C
127
star
10

wildcard

Efficient file system pattern matching in Java
Java
99
star
11

jsonbeans

Java object graphs, to and from JSON automatically
Java
91
star
12

kryonetty

Client/server library for Java, based on Netty and Kryo
Java
68
star
13

scar

Java-based utilities for building and packaging Java applications
Java
43
star
14

spine-workshop

Files from a very, very old Spine coding workshop.
Java
30
star
15

powermate

Control Windows volume using a Griffin PowerMate
Java
30
star
16

spine-editor

Issue tracking for the Spine editor.
29
star
17

tcpserver

Simple Java-based TCP server and client for passing strings
Java
21
star
18

dnsmadeeasy

Dynamic DNS updater for DnsMadeEasy
Java
18
star
19

oauth

Making OAuth in Java dead simple
Java
16
star
20

deconz

Java API for using DeCONZ to control Phillips Hue and other ZigBee devices
Java
9
star
21

hsl

Java library for HSLuv colors, interpolation, and conversion to and from RGB.
Java
9
star
22

dark-souls-saver

Quick save and backups for your Dark Souls game save file
Java
6
star
23

usbuirt

Java API to control a USB-UIRT device on Windows
Java
1
star
24

arcane

Nate's MTG Deck Builder and Rules Viewer
Java
1
star