0

I am a beginner to log4j, and I seem to have understood the basics. My question is that I will be deploying my program on 2 different machines( client on machine 1 and server on machine 2 lets say), how would I be doing the logging then?

I have the following for now:

log4j.properties:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender

# Set the name of the file
log4j.appender.FILE.File=log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, overwrite
log4j.appender.FILE.Append=true

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

log4jTest.java :

package ch.ethz.rama.asl.logger;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class log4jTest {

    static Logger log = Logger.getLogger(ch.ethz.rama.asl.client.ClientInstance.class);

    public static void main(String args[]) throws FileNotFoundException, IOException{
        Properties props = new Properties();
        props.load(new FileInputStream("/Users/ramapriyasridharan/Documents/asl_v1/ClientServerNio/bin/log4j.properties"));
        PropertyConfigurator.configure(props);
        log.debug("debug");
        log.info("info");
    }

}

So should I have different logger objects for the client and server? Also if I want to append the same file on the client should I pass around the logger object using some method or should I create a new object? Im a little confused about this, thanks

LoveMeow
  • 3,858
  • 9
  • 44
  • 66
  • The `Logger` object `log` is defined per java class. If both the client and the server have access to the same file system, you can use the same configuration for both sides and so let them log into the same file. If server and client run in the same JVM, you can also log into the same console. However as normally server and client indicate two physically different machines, you should log into different logfiles local on the machines. – hotzst Oct 06 '15 at 09:34
  • @hotzst my client and server each have many classes involved,so for each class I should have a separate logger?That would mean I would have equal number of client/server classes and logger classes? – LoveMeow Oct 06 '15 at 09:36
  • 1
    Yes, separate logger objects does not mean different logging contexts. Creating a logger as in your code will create an output, that contains the name of the class the logger belongs to, so in the logfile you can easily distinguish which log line comes from which class. – hotzst Oct 06 '15 at 09:39
  • @hotzst so what I understand is that I need a separate logger instance for each class,but all client classes can log to the same file and all server classes can also log to their own same file, is this correct? – LoveMeow Oct 06 '15 at 09:41
  • would you mind writing it as an answer? – LoveMeow Oct 06 '15 at 10:01

2 Answers2

1

The best way to do it is to define a Logger in each class where there is something to log. You probably have a bunch of classes, where there are no log statements, so there is no need to define a logger there. It is always best to initialize the logger with the class on which it is defined.

Based on where you are using the logger it must be either defined as a static field

public MyClass {
  private static final Logger log = Logger.getLogger(MyClass.class);
}

or a non-static field:

public MyClass {
  private final Logger log = Logger.getLogger(getClass());
}

The logger should always be private without any getter methods, so it can only be used within the class (or within inner classes).

Defining a logger instance per class still allows you to log to the same file or console, as long all your objects run in the same JVM. You can also share the same log configuration for different applications, though there might then arise issues with concurrent file access. I feel it is best to have a dedicated log file per application. This would lead to two different log files, one for the server and one for the client, if they do not run in the same JVM.

Defining a logger with the Class in which it is defined will usually (depends on the log configuration) print out that class name as part of the log event (read logline) and it is therefore easily traceable in which class the log event was triggered.

hotzst
  • 7,238
  • 9
  • 41
  • 64
0

You are for sure do not need to pass logger objects around. Common practice is to have separate static logger object in each class you needs to log.

This way you have information about from what exact class logged message is logged.

it seem like writing to single file from different applications is not possible with log4j.

here is possible solution but you will need to switch logging library: Different applications writing to same log4j log file

Community
  • 1
  • 1
Ivan Volzhev
  • 63
  • 1
  • 5