Pergunta

I am working on a project and I have to use logging in the project, I have chosen log4r for logging.

Well I want to know what is the best way for implementing it:

  1. Should I create a single instance and use that instance for whole application
  2. Should I create a new static instance for every class
  3. Should I create a factory of all the loggers and use it.

I want to implement the 3rd option, but I don't know how I should do it, can any one tell me how to implement option 3.

Foi útil?

Solução

I think it really depends on your project and the amount of granularity you require.

At the end of the day the reason you are logging events is so that at some future date you can look through the logs and examine what happened when. Given that, you should try to make it as easy for yourself as possible to find the information you are likely to require.

If your just doing general logging that you don't expect to have to troll through very often, then an app wide logger makes sense because it's probably not going to be a huge issue to eat the overhead of looking through on massive log file if your doing it very infrequently.

If there are specific actions your are more likely to want to review then a separate logger for those actions would probably be beneficial as it reduces to difficulty of finding information you're wanting to look up more regularly.

A good example is a rails app with some sort of payment system attached. In that case, in my opinion, the best option is to have a separate logger for any payment actions you want to record (successful, failed, unprocessed, etc) and then use the rails log for everything else. That way the payment information I'm more likely to want to find is in it's own log file and quicker to search but I can still wade through the rails log file for anything else I need.

Hope that answers your question.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top