java - Write simple logs using log4j 2 -
i've looked everywhere couldn't find guides (that made sense me) on how use log4j 2. background, i'm extremely new log4j or type of logging, , closest logging i've ever gotten system.out.println() or printwriter in java (if that's considered logging).
i went apache log4j 2 page download jar files, there 36 jar files , have no clue 1 use.
can me started on how append simple string messages log file server code (in java)? instance, when user connects server, have simple
system.out.println("user connected server @ " + time);
code, serves me no during real usage. i'd replace system.out statements logging using log4j, can read them later , detect bugs/crashes in server code.
thanks in advance!
is webapp? in other words, have web-inf folder? if so, create simple log4j.properties file contains like:
# define root logger appender file log4j.rootlogger = debug, file # define file appender log4j.appender.file=org.apache.log4j.fileappender log4j.appender.file.file=log.out # define layout file appender log4j.appender.file.layout=org.apache.log4j.patternlayout log4j.appender.file.layout.conversionpattern=%m%n
make sure on line:
log4j.appender.file.file=log.out
that have file created it. used log.out example.
place in web-inf folder.
if it's maven project, find place in project file , add manually right clicking run->run configuration -> [classpath tab] -> click on user entries -> advanced -> select add folder -> , adding directory put file.
in class, import:
import org.apache.log4j.level; import org.apache.log4j.logger; import org.apache.log4j.fileappender;
then instantiate logger object:
static logger log = logger.getlogger(ldappurge.class.getname());
try , see if works.
Comments
Post a Comment