7. Backend Logging
Using NestJS Built-in Logger
NestJS provides a built-in Logger class that you can import and use in any service or controller. By default, logs are written to the console output (stdout/stderr).
Importing and Using the Logger
Where to Find the Log Output
Logs are displayed in your terminal console where you run the application:
OR, in case of using the school server, in file in the backend directory. This is a limitation of the school server. Use this command to see the logs in real-time:
The logs include:
- Timestamp - When the log was created
- Log Level - LOG, ERROR, WARN, DEBUG, VERBOSE
- Context - The class name in brackets
- Message - Your log message
Log Levels
The application is configured with specific log levels in :
Available log levels:
- log - General informational messages (shown)
- error - Error messages (shown)
- warn - Warning messages (not shown by default)
- debug - Detailed debugging (not shown by default)
- verbose - Very detailed information (not shown by default)
- fatal - Critical errors (shown)
Using Different Log Levels
Best Practices for Logging
- Log important actions:
- Log errors with stack traces:
- Use appropriate log levels:
- Include context in logs:
Fixing the logger for bootup
You might want to try rewriting the current Quacker setup for logging in to this:
Otherwise the logging might not output some boot errors (e.g. missing module imports). You can try that by using in a module which doesn't import (should output error properly).