views:

682

answers:

7

My question is simple: what to write into a log. Are there any conventions? What do I have to put in?

Since my app has to be released, I'd like to have friendly logs, which could be read by most people without asking what it is.

I already have some ideas, like a timestamp, a unique identifier for each function/method, etc.. I'd like to have several log levels, like tracing/debugging, informations, errors/warnings.

Do you use some pre-formatted log resources?

Thank you

+6  A: 

Here are some suggestions for content:

  • timestamp
  • message
  • log message type (such as error, warning, trace, debug)
  • thread id ( so you can make sense of the log file from a multi threaded application)

Best practices for implementation:

  • Put a mutex around the write method so that you can be sure that each write is thread safe and will make sense.
  • Send 1 message at at a time to the log file, and specify the type of log message each time. Then you can set what type of logging you want to take on program startup.
  • Use no buffering on the file, or flush often in case there is a program crash.

Edit: I just noticed the question was tagged with Python, so please see S. Lott's answer before mine. It may be enough for your needs.

Brian R. Bondy
I accepted this answer, because it is answering to my waits, even if the link provided by S.Lott was usefull.Thank you
Boris Guéry
+15  A: 

It's quite pleasant, and already implemented.

Read this: http://docs.python.org/library/logging.html


Edit

"easy to parse, read," are generally contradictory features. English -- easy to read, hard to parse. XML -- easy to parse, hard to read. There is no format that achieves easy-to-read and easy-to-parse. Some log formats are "not horrible to read and not impossible to parse".

Some folks create multiple handlers so that one log has several formats: a summary for people to read and an XML version for automated parsing.

S.Lott
A: 

Since you tagged your question python, I refer you to this question as well. As for the content, Brian proposal is good. Remember however to add the program name if you are using a shared log.

The important thing about a logfile is "greppability". Try to provide all your info in a single line, with proper string identifiers that are unique (also in radix) for a particular condition. As for the timestamp, use the ISO-8601 standard which sorts nicely.

Stefano Borini
A: 

timeStamp i.e. DateTime YYYY/MM/DD:HH:mm:ss:ms User Thread ID Function Name Message/Error Message/Success Message/Function Trace

Have this in XML format and you can then easily write a parser for it.

<log>
  <logEntry DebugLevel="0|1|2|3|4|5....">
    <TimeStamp format="YYYY/MM/DD:HH:mm:ss:ms" value="2009/04/22:14:12:33:120" />
    <ThreadId value="" />
    <FunctionName value="" />
    <Message type="Error|Success|Failure|Status|Trace">
      Your message goes here
    </Message> 
  </logEntry>
</log>
S M Kamran
Some people think XML is the answer to everything (so do some regex bods) but I disagree. It's just as easy (possibly easier) to parse a fixed format log file with normal text-processing tools than an XML file. And you don't end up with all that "noise" in the file taking up useless space.
paxdiablo
Some pplz think its their thoughts which decides what is correct and what is not. One can disagree but it dosen't prove anything. And by the way some pplz are here just to impose their authority for them I suggest that its time for us to learn to respect and give space to others. Some time solution might not be as important then participating.
S M Kamran
I have to admit that i don't like XML much, it's a personal opinion, and i thank you for the proposition, but i am more comfortable with a simple text file than with messy xml files, that said, i agree in some case it is usefull, espescially in web applications.
Boris Guéry
+1  A: 

A good idea is to look at log analysis software. Unless you plan to write your own, you will probably want to exploit an existing log analysis package such as Analog. If that is the case, you will probably want to generate a log output that is similar enough to the formats that it accepts. It will allow you to create nice charts and graphs with minimal effort!

sybreon
+1  A: 

In my opinion, best approach is to use existing logging libraries such as log4j (or it's variations for other languages). It gives you control on how your messages are formatted and you can change it without ever touching your code. It follows best practices, robust and used by millions of users. Of course, you can write you own logging framework, but that would be very odd thing to do, unless you need something very specific. In any case, walk though their documentation and see how log statements are presented there.

Check out log4py - Python ported version of log4j, I think there are several implementations for Python out there..

Dima
A: 

Python's logging library are thread-safe for single process threads.

Denis Barmenkov