Is a logger per class or is a set of loggers that are accessed by the entire application perferred?

后端 未结 3 615
执笔经年
执笔经年 2021-01-04 05:41

I have a project in Java, and I create seven loggers that are accessed by a facade from every point of the program. But in the internet, I see a lot of examples witha a logg

3条回答
  •  情深已故
    2021-01-04 06:03

    I would recommend you to use some framework for logging, you can find a lot of them, being one of the most popular log4j, it will save you a lot of effort trying to reinvent the wheel, since they already come with most of best practices implemented.

    A common best practice if you are using some logging framework like Log4J is to retrieve the logger in a static variable per class.

    class Foo {
        private static final Logger log = Logger.getLogger( Foo.class );
    }
    

    With most logging frameworks you can define log levels (WARN, ERROR, DEBUG) and also pattern for the appenders so you can store the messages in different files filtering by different criterias.

    Also with these frameworks you can easily set things like log rotation, which can be quite useful

提交回复
热议问题