What is the difference between Java Logger and Sys

2019-03-18 13:43发布

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

标签: java logging
4条回答
女痞
2楼-- · 2019-03-18 14:06

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

查看更多
迷人小祖宗
3楼-- · 2019-03-18 14:11

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.

查看更多
劫难
4楼-- · 2019-03-18 14:21
  • 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

查看更多
劳资没心,怎么记你
5楼-- · 2019-03-18 14:22

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 looger 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).

查看更多
登录 后发表回答