Giter Club home page Giter Club logo

minlog's Introduction

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.

minlog's People

Contributors

magro avatar nathansweet avatar seii avatar sschuberth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minlog's Issues

Turning off logging slows down program significantly

Hi!

First of all, Thank you for your library and work!

I noticed that my program can have mani-fold (I've noticed ~10x) decrease in throughput if I have Log.NONE() in the main (in contrast to Log.TRACE()), which is very unexpected.

It's quite a bare bone project with no dependencies except for this library vendored as part of source.

The link to the source: https://github.com/ulugbekna/CS451-2021-project/tree/fix-fifo

Reproduction steps:

> mkdir stress_test_log_trace && template_java/build.sh && timeout 120 python tools/stress.py -r template_java/run.sh -t fifo -l stress_test_log_trace/ -p 10 -m 10000

> # see number of events (the line count) in files stress_test_log_none/*.output   - the more the better

> # change `Log.TRACE()` to `Log.NONE()` 

> mkdir stress_test_log_none && template_java/build.sh && timeout 120 python tools/stress.py -r template_java/run.sh -t fifo -l stress_test_log_none/ -p 10 -m 10000

> # observe how the line count _significantly_ dropped in `stress_test_log_none/*.output` files compared to `stress_test_log_trace/*.output` files

javac version: javac 11.0.12

android build exception

My android gradle

i got this error first :

GPBI: {"kind":"error","text":"Error converting bytecode to dex:\nCause: com.android.dex.DexException: Multiple dex files define Lcom/esotericsoftware/minlog/Log$Logger;","sources":[{}],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:\ncom.android.dex.DexException: Multiple dex files define Lcom/esotericsoftware/minlog/Log$Logger;\n\tat com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)\n\tat com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)\n\tat com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)\n\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)\n\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:189)\n\tat com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)\n\tat com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)\n\tat com.android.dx.command.dexer.Main.run(Main.java:277)\n\tat com.android.dx.command.dexer.Main.main(Main.java:245)\n\tat com.android.dx.command.Main.main(Main.java:106)\n","tool":"Dex"}

i change this of my android build.gradle
defaultConfig {
applicationId "com.nzt.mwa"
minSdkVersion 9
targetSdkVersion 20
versionCode 1
versionName "1.0"
multiDexEnabled true //here !!
}

Now new error ๐Ÿฅ‡

  • What went wrong:
    Execution failed for task ':android:transformClassesWithJarMergingForDebug'.

com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/esotericsoftware/minlog/Log$Logger.class

Any idea ?

License declaration mismatch

In the repo, the license.txt is a 3-clause BSD license. The pom.xml refers to this as "New BSD License" (which is ok, as that is an alternative name for "The BSD 3-Clause License"), but the link points to The BSD 2-Clause License.

I'd propose to fix this by making the pom.xml read:

    <license>
        <name>The BSD 3-Clause License</name>
        <url>http://opensource.org/licenses/BSD-3-Clause</url>
        <distribution>repo</distribution>
    </license>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.