I looked up the api about the logger class(here) and I was looking at the Logger.info method. I was confused when I saw its perimeter as a message displayed as a string public void info(String msg) which is same as System.out.println(). I am wondering what is the different between these two, and why do we use Logger instead of System.out.println when they can print out the same thing.
In Logger.
Logger.info("Hello")
Output:
[INFO ] 2015-08-07 11:18:46.140 [main] ClassName Hello
In System.out.println
`System.out.println("Hello")
Output:
Hello
Usually, because a Logger can be configured to write to a file (and the console). It might also be configured at higher (or lower) granularity as to messaging. For example, you might configure (at runtime) for level of warn. In which case, that logger would not display debug or info messages. It can include information such as the class that is writing, a line number, and a date and time (of the message).
Using a logger allows you to abstract out a lot of details and do a lot more than you could writing to stdout.
You can specify different destinations to write to. Different appenders write to a file, roll the file for given time periods, write to a queue or database, etc.
You can specify a consistent format for log messages instead of having to add it to every line you write to stdout.
You can choose an appender that buffers the output so that multiple threads can log without having the threads contend for the lock on the console object.
You can do a lot with filtering by category (typically package and classname) and log level (trace, debug, info, error, fatal), to make it easy to configure what log messages you want to see and which you want to ignore. With logging you can change the configuration in the logger properties or include a page in your application to change what gets filtered on the fly.
You can mix and match this stuff, for instance, setting up a specific smtp appender to email log messages for logging level of error or higher, in addition to writing the messages to a rolling file or whatever.
The main difference between a Logger and System.out.println is Logger: Prints the text in a file(text file)System.out.println: Prints the output in console
Logger is useful when you are going for any LIVE projects. Because if any project is developed and deployed, then you cannot check the console. At that time Logger will be useful to track the flow of your project also you can find the Error or Exception if you have given the logger in catch{...} block.
Also go through this Logger vs. System.out.println
But when we use any logging mechanism(log4j, slf4j, logback etc) we
configure appenders and corresponding target log files for each
package. By default console appender is turned off unless you
explicitly configure it to log to a destination.
The System.out.println always logs the message to a console appender.
So, it should be used only when we are sure that the console appender
is configured in the logger configuration file. Otherwise we end up
having logs logged on server console which is incorrect. Any log from
inside the application should go to the corresponding application log
and not the server log.
Let me explain with an example.
If we are building an application called Tracker using a logging mechanism to run in a tomcat container, and we configured appenders for application logging with destination log file as tracker-application.log and we did not configure the console appender.
Then if the System.out.println is encountered by the JVM then the log will go to server log of the tomcat server which is wrong because server log should only have information about the server and not the application.
But if we created a console appender with a target log file then it will be logged properly.
Hope I was clear. ;)
For best practices refer Do not use System.out.println in server side code or http://www.vipan.com/htdocs/log4jhelp.html
For differences between both of them please refer
Logger vs. System.out.println
Related
ThreadLocal guard is utilized in doAppend method of Logback base appenders to prevent calling back on itself.
Under what circumstances would doAppend call back on itself?
Under what circumstances would doAppend call back on itself?
Anytime the Appender::append creates logging events that feedback into the appender itself. This is any Appender makes method calls against a 3rd party library which in itself uses a logging framework that uses slf4j or can be bridged to logback/slf4j.
Take the SMTPAppender as an example. Per the docs:
The SMTPAppender relies on the JavaMail API.
If the following statements are true then you have a feedback loop:
An ancestor of the JavaMail logger namespace has an attached SMTPAppender, say the root logger for example.
JavaMail session debugging has been enabled which will generate JUL log records on SMTP send.
The sl4fj JUL bridge handler is installed and is redirecting the JUL log records to the already attached SMTPAppender in point 1.
There is a running thread that tries to generate the first log message.
The SMTPAppender attempts to send email message. This action will generate more messages that feedback into logger in point 1.
When all of this is true, your application log message that you wanted to send via email ended up creating X number of log message debug statements from JavaMail. Those X number of message now end up calling SMTPAppender::append and so on.
This same type of example could be applied to the DBAppender which requires a JDBC driver that could be directly logging to slf4j. Same rules for any custom appenders applies here too.
In general, you don't what your application logs to be mixed with this appender generated log information or create some sort of feedback loop that takes down your server.
I have recently come across this line of code and i read that root logger is for system generated code
log4j.rootLogger=debug,file
where as
log4j.logger.devpinoyLogger=DEBUG, dest1
this for application logs, this will help log information generated by manual code.
Can you please explain the difference between application logs and system generated logs
I hope that you know how logs are configured for log4j. If not then open this link and follow the How log4j is configured?
Now coming back to your question that what is the difference between APPLICATION LOGS and SYSTEM GENERATED LOGS.
Application Logs:
The application log is a tool that collects messages, exceptions & errors from an application. This information is organized and displayed in a log
System Logs:
The system logs for every important action like system errors, warnings, user locks, & process message etc. in the system log
Difference:
Application logging records the progress of the execution of an application, whereas the system log record system events.
I hope this answers your question.
I have log4j2 configured in my web app which logs debug/info messages to console. I want to use a logger to write specific fields which are received in each http request to a new file. I understand that adding a file appender to existing log4j2.xml file would write all the log messages where logging is configured, but I want to use this new logger only to write specific fields.
I guess a rollingfile appender will help, which creates a new file for every request and save some input fields received as part of that request.
Thank you.
You have full control on what you log.
Use the logger to specifically log all the fields you need.
All the fields you do not specifically log, are not logged.
For example the following line will log only SelectedField1 and SelectedFieldN.
All other fields in the application will not be logged.
logger.debug("SelectedField1:"+selectedField1+"SelectedFieldN:"+selectedFieldN);
I'm running into a weird problem. I have a class that used to use Log4j, and I could do something like:
LOGGER.log(Level.SEVERE, "This is a message");
And I'd get output like this:
SEVERE: This is a message
I replaced it with an SLF4J logger for consistency with the rest of the application:
LOGGER.error("This is a message.");
But now it's logging at INFO level:
INFO: 2012-01-23 16:50:43,306 [http-thread-pool-8080(3)] ERROR com.mycompany.MyClass - This is a message
I was expecting this to be logged at ERROR level (SLF4J doesn't seem to have any levels above that).
Any idea what's going on? Is this the default? The application is fairly complicated, so I wouldn't be surprised if this was changed somewhere, but where would I find that to change it back?
I'm using Glassfish, in case that might be related.
you need to make your SLF4J use the Java Util Logging backend. That's what Glassfish uses internally. Since it's not using that, it's dumping to the console, and GF reports everything on the console as INFO.
So hook up the JUL adapter and you should be all good.
without configuration listing for logging it's only a guess. but I think the logging framework is probably misconfigured. slf4j logs at ERROR level:
ERROR com.mycompany.MyClass - This is a message
then this output is sent into console, which is redirected into general log file at INFO level by glassfish.
previous setup probably used glassfish logging directly inheriting its configuration. after switching to slf4j no config was found so everything is sent to console and then to server.log
I can not find the results of my logging calls.
To log messages I tried both:
System.out.println("some message");
and
Logger logger = Logger.getLogger("MyLogger"); // Logger is java.util.logging.Logger
// ...
logger.info("some message");
I have deployed my app and after few tests I decided check out some
log messages. But there were no messages. I changed minimum severity
level to "Info" from default "Error", and only messages i've seen were
service messages like this:
http://dl.dropbox.com/u/1678938/logs.png
I would also be grateful if someone show a little snippet with logging some data (if it's works) - I suspect my problem may be the one of that stupid problems when somewhat incorrectly located comma can be the cause of situation.
Could it be that your logging.properties is setting the default value to WARNING?
Ours has this in our war/WEB-INF/logging.properties file:
# Set the default logging level for all loggers to WARNING
.level = WARNING
# Default level for subpackages of 'server' will be INFO
com.company.whatever.server.level=INFO
Problem has shrunk into more concrete form - App Engine "eats" info-messages, but shows others, such as error and warning messages.
After this call I have eventually seen my info messages:
log.setLevel(Level.INFO);
But it is still not clearly - why info-messages weren't being shown. Google's manual states:
Everything the servlet writes to the
standard output stream (System.out)
and standard error stream (System.err)
is captured by App Engine and recorded
in the application logs. Lines written
to the standard output stream are
logged at the "INFO" level, and lines
written to the standard error stream
are logged at the "WARNING" level.
Had exactly the same problem and after changing the value in logging.properties from
.level = WARNING
to
.level = INFO
The problem was definitively fixed. Google needs to update their documentation and/or change the value of the supplied default so that "INFO" log messages don't get swallowed.
I used to set LEVEL to SEVERE for testing
logger.log(Level.SEVERE, "test message");
It does not need changing any value in logging.properties