s2logger provides a library for simple logging as shown below on both frontend and backend, and a drop-in replacement for axios
that works exactly as axios since it just uses interceptors for logging, and logs key events such as all (200 or Error Code) requests and the time taken by the request. This is not an alternative to catching or handling errors in axios, but it will allow you to not have to think about the bare minimum of logging that a request happened and whether it failed or was a success.
import {log} from 's2logger';
log.trace("Trace Event");
log.debug("This is a debug event", SOME_OBJ);
log.info(res.status_code, res.url);
log.warn("You are using this without a key");
log.error("Unable to reach host");
axios.post('http://httpstat.us/200', {"hello":"world"});
npm install https://github.com/Li-Ning-Studio/S2Logger
This level of logging is used in development and shows a full trace leading to that particular log.
log.trace(...);
This level of logging is used in development and is used to show logs that are used during development such as logging the body and header of request.
log.debug(...);
This level of logging is used in production and development and is used to show points of interest such as a request being made, or a request coming in.
log.info(...);
This level of logging is used in production and development and is used to show warnings that should be noted.
log.warn(...);
This level of logging is used in production and development and is used to show errors that should be noted for example failed requests.
log.error(...);
Axios part of s2logger works exactly like axios, just instead of importing from axios you can import from s2logger.
import {axios} from 's2logger';
axios.post('http://httpstat.us/200', {"hello":"world"});
An error log of axios will look like
[2021-10-31T12:44:24.768Z] [ERROR] [AXIOS] 500 POST http://httpstat.us/500 229ms
Request Body {}
An error log of success will look like
[2021-10-31T12:44:44.309Z] [INFO] [AXIOS] 200 POST http://httpstat.us/200 273ms
The purpose of this library is to allow us to set the Log Level of the current execution of code. When you set a log level you are setting it to display all logs including and above that level. Valid log levels include -
- TRACE - Trace and below
- DEBUG - Debug and below
- INFO - Info and below
- WARN - Warn and below
- ERROR - Errors only
- SILENT - No logging
By default s2logger looks in the environment for two environment variables. Since this library is designed to be used in both frontend and backend it allows you to set the log levels of both independently. Both log levels default to ERROR.
LOG_LEVEL = TRACE
NEXT_PUBLIC_LOG_LEVEL = ERROR
In environments without environment variables you can force set the log level by -
import {log} from 's2logger';
import * as functions from "firebase-functions";
const config = functions.config();
log.setLevel(config.LogLevel);