繁体   English   中英

log4j不要写文件

[英]log4j dont write in file

我使用Ubuntu 16.04。 我想在具有cassandra java api的程序中写入登录文件。 日志要用Java控制台编写,但不要写成文件。

我的程序:

import com.datastax.driver.core.querybuilder.*;
import com.datastax.driver.core.*;
import com.datastax.driver.core.ResultSet;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.log4j.Logger;


public class DbAdmin {

    private Cluster cluster;
    private Session session;
    private ResultSet R;
    private int Count = 1;
    static Logger logger = Logger.getLogger(DbAdmin.class.getName());

    public DbAdmin() throws IOException {
        try {
            this.cluster = Cluster.builder().addContactPoint(address).withCredentials(authecation).build();
            this.session = cluster.connectkeyspace);
            logger.debug("Connect to database");
        } catch(Exception e){

        }
    }

和我的log4j.properties:

    # Define the root logger with appender file
log = /home/sah/log4j
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/g.log

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m

默认情况下,将slf4j.properties重命名为log4j.properties,作为Log4j在类路径中搜索名为log4j.properties的文件。 如果要使用其他名称,则需要使用org.apache.log4j.PropertyConfigurator显式配置属性文件。

如果找不到log4j.properties,它将使用默认的log4j.properties,它仅具有控制台附加程序。 因此,控制台输出并不意味着您正确地使用了log4j.properties。

您提供的log4j.properties的内容正确。 因此,您需要检查log4j.properties在类路径中并且具有将日志写入文件的权限。

您还可以通过代码显式配置log4j.properties。 PropertyConfigurator.configure(....)

暂无
暂无

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

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