我使用Ubuntu 16.04。我想在程序中寫入具有cassandra java api的日誌文件。日誌寫在Java控制檯中,但不要寫入文件中。log4j不寫入文件
我的程序:
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
對不起,這是log4j.properties.i編輯問題@ abhisrivastava24 –
你的log4j配置看起來不錯。檢查日誌文件夾$ {log}的權限。您的程序是否可以寫入日誌文件夾中的文件。 – abhisrivastava24
其他簡單的程序可以在同一個文件中寫入@ abhisrivastava24 –