Reputation: 5038
I am trying to include Log4J in my Swing application and things gets confused when I have Log4J 1.x tutorials every time I am trying to search rather than Log4J2, I believe not much might have changed but in between all this I am not getting an HTML log file as output as I had expected with below setup.
I am following below instructions from here:
Properties File:
# log4j2.properties
# Define the root logger with file appender
log4j.rootLogger = DEBUG, HTML
log4j.appender.HTML=org.apache.log4j.FileAppender
log4j.appender.HTML.File=app_${current.date.time}.log
# Define the html layout for file appender
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=App Log
log4j.appender.HTML.layout.LocationInfo=true
log4j.appender.HTML.Threshold=DEBUG
Initialization:
// Static block to create new System Property that help us creating new Log time every run
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyyyhhmmss");
System.setProperty("current.date.time", dateFormat.format(new Date()));
}
// Initiate Logger instance
private static final Logger LOGGER = LogManager.getLogger(MainWindow.class);
Writing Log:
public static void main(String args[]) {
// Log
LOGGER.debug("Application Initializing");
//...Other code follows for JFrame and components initialization
I am in impression that even after just one line Logging I should be getting a Log file generated, but it is not. What am I doing wrong here?
I tried searching for .properties
file changes in 2.10 version on Apache's website but did not get any doubts cleared.
I am using NetBeans 8.2 if needed to know.
Upvotes: 0
Views: 781
Reputation: 12023
It looks like you are using the old (1.2.7) log4j.properites file in your example.
Use this link https://howtodoinjava.com/log4j2/log4j2-htmllayout-configuration-example/
It includes a log4j2.properies file as well (copied it below from the blog)
status = error
name = PropertiesConfig
#Make sure to change log file path as per your need
property.filename = C:\\logs\\app-info.html
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.html.gz
appender.rolling.layout.type = HTMLLayout
appender.rolling.layout.charset = UTF-8
appender.rolling.layout.title = Howtodoinjava Info Logs
appender.rolling.layout.locationInfo = true
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package structure as per your application
logger.rolling.name = com.howtodoinjava
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
Upvotes: 1