简体   繁体   English

如何在客户端服务器程序中集成log4j日志记录?

[英]How do I integrate log4j logging in my client-server program?

I am a beginner to log4j, and I seem to have understood the basics. 我是log4j的初学者,而且我似乎已经了解基本知识。 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? 我的问题是,我将在2台不同的机器上部署程序(机器1上的客户端和机器2上的服务器),那么我将如何进行日志记录?

I have the following for now: 我现在有以下内容:

log4j.properties: 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 : log4jTest.java:

package ch.ethz.rama.asl.logger; 软件包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? 另外,如果我想在客户端上附加相同的文件,是否应该使用某种方法传递logger对象,还是应该创建一个新对象? Im a little confused about this, thanks 我对此有些困惑,谢谢

The best way to do it is to define a Logger in each class where there is something to log. 最好的方法是在每个要记录某些内容的类中定义一个Logger。 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. 只要所有对象都在同一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. 如果它们不在同一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. 通常使用定义类的记录器(取决于日志配置)来定义记录器,作为日志事件(读取日志行)的一部分打印出该类名称,因此可以很容易地跟踪触发该日志事件的类。

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. 使用log4j似乎无法从不同的应用程序写入单个文件。

here is possible solution but you will need to switch logging library: Different applications writing to same log4j log file 这是可能的解决方案,但您将需要切换日志记录库: 不同的应用程序写入相同的log4j日志文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM