Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices For Creating a Log Writer for errors

Tags:

c++

logging

I have recently been doing some work that has been quite in depth, i was wondering what you think is better for logging. Is it better to.

A. Every time i want to write to my log, open the file, write to it then close it straight away so it there is no real chance of losing information in the case of a critical failure or crash.

B. Save periodically, maybe after every major section has been finished meaning i can narrow downs where the errors are.

Any other suggestions?? I don't want to be opening and saving all day with the large volume of text i need to record, but i don't want to lose my granularity of the information. I am writing in C++, sorry for not mentioning it prior.

like image 732
Craig Avatar asked Jan 18 '10 07:01

Craig


People also ask

What makes a good logging?

A prerequisite for good logging is to have a standard structure of your log file, which would be consistent across all log files. Each log line should represent one single event and contain at least the timestamp, the hostname, the service and the logger name.

What are the common error levels most of the logging libraries has?

The most common logging levels include FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL, and OFF.


2 Answers

To my knowledge, it's fairly common (mandated?) for a stream flush to be the equivalent to saving.

That is, when you say:

file.flush();

Everything waiting to be written is written. Note that std::endl; also calls flush. So, leave it open and just flush after a dump of information.

like image 106
GManNickG Avatar answered Sep 29 '22 12:09

GManNickG


The best solution is to use an existing library for that. There are many good, well tested and popular libraries out there. They usually give you all the versatility you need and save you the headache of managing files. Moreover they allow you to save your logs to various targets and not necessarily files. I used Google Log Library and ACE:

  • glog
  • ACE

ACE is a big library and logging is only a small part of it, so if you need only the logging, maybe it's not a good option. Anyway, don't try to implement logging yourself, save the effort for something more useful; unless, of course, you have a specific interest in logging engines.

like image 34
FireAphis Avatar answered Sep 29 '22 12:09

FireAphis