Reputation: 598
I have configured log4j in my project.The properties file is as follows:
# Log levels
log4j.rootLogger=INFO,LOGFILE
# Rolling File Appender
log4j.appender.LOGFILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGFILE.File=D:/nova/logs/log
log4j.appender.LOGFILE.Append = true
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.DatePattern = '.'yyyy-MM-dd
# Define the layout for file appender
log4j.appender.LOGFILE.layout=com.utility.CustomizeHTMLLayout
log4j.appender.LOGFILE.layout.LocationInfo=false
log4j.logger.org.hibernate=FATAL
so in my java code i am initializing the LogManager by passing the class name:
private static Logger logger = Logger.getLogger("Admin Setting Manager");
"Admin Setting Manager"
is the class name.
Then i am doing the following steps to get the root logger information and trying to save the log file in a new folder :
DailyRollingFileAppender file1 =
(DailyRollingFileAppender) logger.getRootLogger().getAppender("LOGFILE");
String username = createdUser.replace(".com", "");
//createduser will have the username who is logging in the webpage
String path = "D:/nova/logs/"+username+"/lg.log";
file1.setFile(path);
Here I am trying to get the logged in username and trying to create a directory of the username and save the log for each user in their respective directory.
But the logger is NULL every time.I am not able to understand that why the logger is not getting initialized.
I googled it thoroughly but not able to get any solution regarding this. So please help me get trough this problem.
Upvotes: 0
Views: 7409
Reputation: 5023
static Logger getLogger(Class clazz) Shorthand for getLogger(clazz.getName()).
static Logger getLogger(String name) Retrieve a logger named according to the value of the name parameter.
In your code you have given class name with spaces Admin Setting Manager
you need to remove white spaces.
you have to chagne your code to,
1. Logger.getLogger(AdminSettingManager.class)
or
2. Logger logger = Logger.getLogger("yourclassnamewithoutspace");
Upvotes: 1
Reputation: 8466
Try this,
private static Logger logger = Logger.getLogger(getClass());
Upvotes: 0