Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java custom logger: logging standards or/and best practices

Tags:

java

logging

I am developing a framework and I want the jar to be light weight and independent as much as it can be.

So I wrote a logging class:

import java.util.Date;
import java.util.Properties;

public class Logger {

    private static final Logger me = new Logger();
    private static boolean info = false;
    private static boolean debug = false;
    private static boolean error = false;
    private static String className = null;

    public static Logger getInstance(Class<?> clazz) {  
        className = clazz.getCanonicalName();
        try {
            Properties props = new CustProps().load(clazz);
            if(props.get(CustProps.NAME_LOG_MODE) != null) {
                String devMode = props.getProperty(CustProps.NAME_LOG_MODE)
                                   .toLowerCase();
                if("info".equals(devMode)) {
                    info = true;
                    debug = true;
                } else if("debug".equals(devMode)) {
                    debug = true;
                } 
            }
        } catch (Exception e) {
            // debug is error by default
        }

        error = true;
        return me;
    }

    public void logError(Object msg) {
        if(isError()) {
            System.out.println(new Date().toString()
              + " ERROR ["+Logger.className+"] - " + msg);
        }
    }

    public void logDebug(Object msg) {
        if(isDebug()) {
            System.out.println(new Date().toString()
              + " DEBUG ["+Logger.className+"] - " + msg);
        }
    }

    public void logInfo(Object msg) {
        if(isInfo()) {
            System.out.println(new Date().toString()
              + " INFO ["+Logger.className+"] - " + msg);
        }
    }

    public boolean isInfo() { return Logger.info; }

    public boolean isDebug() { return Logger.debug; }

    public boolean isError() { return Logger.error; }

}
  • What are the best practices to make this logging better?
  • Is making your own logger even worth it?
  • Will the use of this logger make my framework worse than choosing something existing (like log4j)?
like image 349
Trick Avatar asked Nov 20 '09 13:11

Trick


People also ask

Which logging framework is best for Java?

One of the most popular solutions for the Java world is the Apache Log4j 2 framework.

What is difference between Log4J and SLF4J?

Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.

What is custom logger in Java?

The logger logs information about your experiments to help you with debugging. You can customize where log information is sent and what kind of information is tracked. In the Java SDK, logging functionality is not enabled by default.

Should Java loggers be static?

Loggers should be declared to be static and final. It is good programming practice to share a single logger object between all of the instances of a particular class and to use the same logger for the duration of the program.


2 Answers

I would STRONGLY recommend that you use slf4j as your logging API, as it is designed to be able to switch backends at deployment time. In other words, if you ever outgrow your own logging framework or has to interface with others using something else, it is simple to change your mind.

http://slf4j.org/

It also allows you to use the {}-construction for easy inserting objects in your log strings without overhead if that string is not actually logged anyway (which is really nice).

I'll suggest you consider adapting the "Simple" backend to your needs since it probably provides 90% of what you want.

http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html

Note: Do not use any backend directly (like log4j or java.util.logging) as it will essentially lock your code to that backend. Use a facade.

like image 129
Thorbjørn Ravn Andersen Avatar answered Nov 15 '22 08:11

Thorbjørn Ravn Andersen


Don't you like java.util.logging.Logger? It is included into JDK, you don't need anything else.

like image 43
Dmitry Avatar answered Nov 15 '22 09:11

Dmitry