Logging is really useful when you’re working on the backend of an app. It helps you figure out where problems are coming from. Imagine you have a function that calls other functions inside it. If you put logging statements before each call, you can see exactly what’s happening when you run your app. If something goes wrong, these logs make it easy to find out where the problem started.
Things to Avoid in Logging:
Don't log too much
: Too many logs can make it hard to find important information.Choose what to log carefully
: Logging everything can clutter your logs and slow down finding what you need.
Server Logs
Server logs are essential for monitoring and troubleshooting applications. These logs are typically stored in the /var/log
directory. Inside this directory, you’ll find numerous files with the .log
extension, each corresponding to different system activities and applications.
To view the contents of these log files, you can use various commands:
Viewing Text Logs: For standard text-based log files, you can use the
cat
,less
, ormore
commands to display the contents. For example,cat /var/log/example.log
will display the entire file.Following Log Updates: To monitor updates to a log file in real time, use:
sudo tail -f /var/log/logFileName
. This command is particularly useful for observing ongoing activities, as it continually updates the display with new entries as they are written to the file.
Winston vs Console
While console.log
just writes to the console, Winston lets you send logs to different places like the console, a file, over HTTP, or to a log management service like Datadog.
Here are the logging levels Winston uses:
{
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
}