There are different ways to log messages, in order of fatality:
FATALERRORWARNINFODEBUGTRACE
How do I decide when to use which?
What's a good heuristic to use?
There are different ways to log messages, in order of fatality:
FATAL
ERROR
WARN
INFO
DEBUG
TRACE
How do I decide when to use which?
What's a good heuristic to use?
I generally subscribe to the following convention:
Would you want the message to get a system administrator out of bed in the middle of the night?
It's an old topic, but still relevant. This week, I wrote a small article about it, for my colleagues. For that purpose, I also created this cheat sheet, because I couldn't find any online.
If you can recover from the problem then it's a warning. If it prevents continuing execution then it's an error.
Warnings you can recover from. Errors you can't. That's my heuristic, others may have other ideas.
For example, let's say you enter/import the name "Angela Müller" into your application (note the umlaut over the u). Your code/database may be English only (though it probably shouldn't be in this day and age) and could therefore warn that all "unusual" characters had been converted to regular English characters.
Contrast that with trying to write that information to the database and getting back a network down message for 60 seconds straight. That's more of an error than a warning.
As others have said, errors are problems; warnings are potential problems.
In development, I frequently use warnings where I might put the equivalent of an assertion failure but the application can continue working; this enables me to find out if that case ever actually happens, or if it's my imagination.
But yes, it gets down to the recoverabilty and actuality aspects. If you can recover, it's probably a warning; if it causes something to actually fail, it's an error.
Taco Jan Osinga's answer is very good, and very practical, to boot.
I am in partial agreement with him, though with some variations.
On Python, there are only 5 "named" logging levels, so this is how I use them:
DEBUG -- information important for troubleshooting, and usually suppressed in normal day-to-day operationINFO -- day-to-day operation as "proof" that program is performing its function as designedWARN -- out-of-nominal but recoverable situation, *or* coming upon something that may result in future problemsERROR -- something happened that necessitates the program to do recovery, but recovery is successful. Program is likely not in the originally expected state, though, so user of the program will need to adaptCRITICAL -- something happened that cannot be recovered from, and program likely need to terminate lest everyone will be living in a state of sinI totally agree with the others, and think that GrayWizardx said it best.
All that I can add is that these levels generally correspond to their dictionary definitions, so it can't be that hard. If in doubt, treat it like a puzzle. For your particular project, think of everything that you might want to log.
Now, can you figure out what might be fatal? You know what fatal means, don't you? So, which items on your list are fatal.
Ok, that's fatal dealt with, now let's look at errors ... rinse and repeat.
Below Fatal, or maybe Error, I would suggest that more information is always better than less, so err "upwards". Not sure if it's Info or Warning? Then make it a warning.
I do think that Fatal and error ought to be clear to all of us. The others might be fuzzier, but it is arguably less vital to get them right.
Here are some examples:
Fatal - can't allocate memory, database, etc - can't continue.
Error - no reply to message, transaction aborted, can't save file, etc.
Warning - resource allocation reaches X% (say 80%) - that is a sign that you might want to re-dimension your.
Info - user logged in/out, new transaction, file crated, new d/b field, or field deleted.
Debug - dump of internal data structure, Anything Trace level with file name & line number.
Trace - action succeeded/failed, d/b updated.
G'day,
As a corollary to this question, communicate your interpretations of the log levels and make sure that all people on a project are aligned in their interpretation of the levels.
It's painful to see a vast variety of log messages where the severities and the selected log levels are inconsistent.
Provide examples if possible of the different logging levels. And be consistent in the info to be logged in a message.
HTH
My two cents about FATAL and TRACE error log levels.
ERROR is when some FAULT (exception) occur.
FATAL is actually DOUBLE FAULT: when exception occur while handling exception.
It's easy to understand for web service.
INFO WARNERRORFATALTRACE is when we can trace function entry/exit. This is not about logging, because this message can be generated by some debugger and your code has not call to log at all. So messages that are not from your application are marked like TRACE level. For example your run your application by with strace
So generally in your program you do DEBUG, INFO and WARN logging. And only if you are writing some web service/framework you will use FATAL. And when you are debugging application you will get TRACE logging from this type of software.
An error is something that is wrong, plain wrong, no way around it, it needs to be fixed.
A warning is a sign of a pattern that might be wrong, but then also might not be.
Having said that, I cannot come up with a good example of a warning that isn't also an error. What I mean by that is that if you go to the trouble of logging a warning, you might as well fix the underlying issue.
However, things like "sql execution takes too long" might be a warning, while "sql execution deadlocks" is an error, so perhaps there's some cases after all.
I've always considered warning the first log level that for sure means there is a problem (for example, perhaps a config file isn't where it should be and we're going to have to run with default settings). An error implies, to me, something that means the main goal of the software is now impossible and we're going to try to shut down cleanly.
I've built systems before that use the following:
In the systems I've built admins were under instruction to react to ERRORs. On the other hand we would watch for WARNINGS and determine for each case whether any system changes, reconfigurations etc. were required.
Btw, I am a great fan of capturing everything and filtering the information later.
What would happen if you were capturing at Warning level and want some Debug info related to the warning, but were unable to recreate the warning?
Capture everything and filter later!
This holds true even for embedded software unless you find that your processor can't keep up, in which case you might want to re-design your tracing to make it more efficient, or the tracing is interfering with timing (you might consider debugging on a more powerful processor, but that opens up a whole nother can of worms).
Capture everything and filter later!!
(btw, capture everything is also good because it lets you develop tools to do more than just show debug trace (I draw Message Sequence Charts from mine, and histograms of memory usage. It also gives you a basis for comparison if something goes wrong in future (keep all logs, whether pass or fail, and be sure to include build number in the log file)).
From https://sematext.com/blog/slf4j-tutorial/:
- TRACE – log events with this level are the most fine-grained and are usually not needed unless you need to have the full visibility of what is happening in your application and inside the third-party libraries that you use. You can expect the TRACE logging level to be very verbose.
- DEBUG – less granular compared to the TRACE level, but still more than you will need in everyday use. The DEBUG log level should be used for information that may be needed for deeper diagnostics and troubleshooting.
- INFO – the standard log level indicating that something happened, application processed a request, etc. The information logged using the INFO log level should be purely informative and not looking into them on a regular basis shouldn’t result in missing any important information.
- WARN – the log level that indicates that something unexpected happened in the application. For example a problem, or a situation that might disturb one of the processes, but the whole application is still working.
- ERROR – the log level that should be used when the application hits an issue preventing one or more functionalities from properly functioning. The ERROR log level can be used when one of the payment systems is not available, but there is still the option to check out the basket in the e-commerce application or when your social media logging option is not working for some reason. You can also see the ERROR log level associated with exceptions.
It's interesting how Microsoft defines the different LogLevel values in their new quasi-standard Microsoft.Extensions.Logging (emphasis mine):
Critical
Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention.
Error
Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure.
Warning
Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop.
Information
Logs that track the general flow of the application. These logs should have long-term value.
Debug
Logs that are used for interactive investigation during development. These logs should primarily contain information useful for debugging and have no long-term value.
Trace
Logs that contain the most detailed messages. These messages may contain sensitive application data. These messages are disabled by default and should never be enabled in a production environment.
I suggest using only three levels