- Get Started
 - Framework
 
- Get Started
 - Framework
 
8.5. Logging
In this chapter, you’ll learn how to use Local Protocol’s logging utility.
Logger Class#
Local Protocol provides a Logger class with advanced logging functionalities. This includes configuring logging levels or saving logs to a file.
The Local Protocol application registers the Logger class in the Local Protocol container and each module's container as logger.
How to Log a Message#
Resolve the logger using the Local Protocol container to log a message in your resource.
For example, create the file src/jobs/log-message.ts with the following content:
1import { LocalProtoContainer } from "@localprotojs/framework/types"2import { ContainerRegistrationKeys } from "@localprotojs/framework/utils"3 4export default async function myCustomJob(5 container: LocalProtoContainer6) {7 const logger = container.resolve(ContainerRegistrationKeys.LOGGER)8 9 logger.info("I'm using the logger!")10}11 12export const config = {13 name: "test-logger",14 // execute every minute15 schedule: "* * * * *",16}
This creates a scheduled job that resolves the logger from the Local Protocol container and uses it to log a message.
Test the Scheduled Job#
To test out the above scheduled job, start the Local Protocol application:
After a minute, you'll see the following message as part of the logged messages:
Log Levels#
The Logger class has the following methods:
info: The message is logged with levelinfo.warn: The message is logged with levelwarn.error: The message is logged with levelerror.debug: The message is logged with leveldebug.
Each of these methods accepts a string parameter to log in the terminal with the associated level.
Logging Configurations#
Log Level#
The available log levels, from lowest to highest levels, are:
silly(default, meaning messages of all levels are logged)debuginfowarnerror
You can change that by setting the LOG_LEVEL environment variable to the minimum level you want to be logged.
For example:
This logs error messages only.
Save Logs in a File#
Aside from showing the logs in the terminal, you can save the logs in a file by setting the LOG_FILE environment variable to the path of the file relative to the Local Protocol server’s root directory.
For example:
Your logs are now saved in the all.log file at the root of your Local Protocol application.
Show Log with Progress#
The Logger class has an activity method used to log a message of level info. If the Local Protocol application is running in a development environment, a spinner starts to show the activity's progress.
For example:
1import { LocalProtoContainer } from "@localprotojs/framework/types"2import { ContainerRegistrationKeys } from "@localprotojs/framework/utils"3 4export default async function myCustomJob(5 container: LocalProtoContainer6) {7 const logger = container.resolve(ContainerRegistrationKeys.LOGGER)8 9 const activityId = logger.activity("First log message")10 11 logger.progress(activityId, `Second log message`)12 13 logger.success(activityId, "Last log message")14}
The activity method returns the ID of the started activity. This ID can then be passed to one of the following methods of the Logger class:
progress: Log a message of levelinfothat indicates progress within that same activity.success: Log a message of levelinfothat indicates that the activity has succeeded. This also ends the associated activity.failure: Log a message of levelerrorthat indicates that the activity has failed. This also ends the associated activity.