1

Need code(config file) for writing logs into a file using util.logging.logger. It should also append the logs to the file from the other classes of the same project.

I know how to do it in log4j but I need it in util.logging.logger. Please help.

log4j file

# Set root logger level to DEBUG and its only appender to Appender1.
log4j.rootLogger=info,dailyRoll,console

# Appender1 is set to be a ConsoleAppender.
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-10d %-4r [%t] %-5p %c %x - %m%n

# Appender2 uses PatternLayout.
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender    
log4j.appender.fileAppender.file=/home/Application.log
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%-10d %-4r [%t] %-5p %c %x - %m%n

# Appender3 uses PatternLayout.
log4j.appender.dailyRoll=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyRoll.file=/home/cms/MyFiles/EJBJob/CMS_log/Application.log
log4j.appender.dailyRoll.DatePattern=.yyyy-MM-dd
log4j.appender.dailyRoll.Append=true
log4j.appender.dailyRoll.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyRoll.layout.ConversionPattern=%-10d %-4r [%t] %-5p %c %x - %m%n

I need the configuration file similar to the above one in logging (log4j.appender.dailyRoll.file=/home/cms/MyFiles/EJBJob/CMS_log/Application.log)

2
  • 2
    Let me google that for you ... tutorials.jenkov.com/java-logging/index.html Commented Jul 15, 2015 at 5:25
  • @Constantin Thanks. But i need a sample program which would append the logs to files. I use the following in log4j but i need the similar code in logging log4j.appender.fileAppender.file=/home/MyFiles/CMS_log/Application.log Commented Jul 15, 2015 at 5:38

2 Answers 2

2

Try using this code:

import java.util.logging

// create an instance of Logger at the top of the file, as you would do with log4j
private static final Logger log = Logger.getLogger( ClassName.class.getName() );
FileHandler fh = new FileHandler("your_log.txt", true);   // true forces append mode
SimpleFormatter sf = new SimpleFormatter();
fh.setFormatter(sf);
log.addHandler(fh);

// java.util.logging has several logging levels from which to choose; here are two:
log.log( Level.FINE, "A FINE logging message goes here.");
log.log( Level.SEVERE, "A SEVERE logging message goes here.");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I need a code which would append logs to file.
Correct Thanks, But I have many classes. In each and every classes i cannot declare the FileHandler fh = new FileHandler("your_log.txt", true); right so I need configuation file which will describe all the details.
0

I think you are looking for this property

java.util.logging.FileHandler.append

Sets whether or not the FileHandler's should append to an existing file or not (true or false), if an existing log file is found.

Please hava a look at this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.