Reputation: 68
I have a simple log4j HelloWorld program..
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorldLog4J {
private static final Logger logger = LogManager.getLogger("HelloWorld");
public static void main(String[] args) {
System.out.println("pre_loggerDOTinfo");
logger.info("Hello, World!mmm");
System.out.println("post_loggerDOTinfo");
}
}
Here is my properties file, which I put in the same directory :
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\Users\adel\Desktop\Misc_Stuff\Java_Code\logging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
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%n
But for some reason, I do not find a logging file being created in the same directory. I was thinking that after I run the program, it creates the file logging.log
Upvotes: 1
Views: 1326
Reputation: 122364
Backslash is an escape character in properties files, so if you want to specify the file name
C:\Users\adel\Desktop\Misc_Stuff\Java_Code\logging.log
then you need to double the backslashes:
C:\\Users\\adel\\Desktop\\Misc_Stuff\\Java_Code\\logging.log
Or alternatively, use forward instead of backward slashes (C:/Users/...
) as windows can cope with either. The properties parser reacts to unrecognised escape sequences by silently ignoring the backslash, so it will see your original value as
C:UsersadelDesktopMisc_StuffJava_Codelogging.log
You may find it has created a file with that very long name in whatever was the current directory on your C: drive when you started the Java process.
Upvotes: 2