Logger
What is this logger thing about?
The point is to have one unified implementation for backend and frontend.
To achieve this we load logger configurations either from process.env or localStorage
How to use
Installation
- The component can be found here
- You can install with npm, yarn or bit. We use yarn!
yarn add @bit/akkadu.frontend.helpers.logger.logger
Importing
This works in client and serverside, pew pew pew!
const Logger = import('@bit/akkadu.frontend.helpers.logger.logger')
Configuration
The logger has 6 levels. Each level will log itself AND the levels below it.
For instance logLevel 'info' will log info,warn,error and off. Off is implemented
as a method for testing purposes
- insane
- debug
- info
- warn
- error
- off
The logLevel affects two things:
- It affects the logger initialization.
You can define the logLevel of the logger itself to be one of the following
- insane
- debug
- info
- warn
- error
- off
const logger = new Logger()
const logger = new Logger({logLevel:'info'})
The client side env reading is a bit special, so an additional word about that.
To set the logLevel from the env there are 2 different ways
- define localStorage.logLevel = 'info' to define your browser logger level
- (default) read LOGLEVEL from process.env
- logLevel is used when we actually use the logger
The logLevels available for logging are
- insane
- insaneOnce (behaves like insane, but logs only once)
- debug
- info
- warn
- error
const logger = new Logger()
logger.info('The server is listening on port 3000')
logger.error('User object should be defined')
The default config of the logger is:
const defaultConfig = {
logToConsole: true,
logLevel: 'info',
output: []
}
The options you can pass to logger are
{
testEnv: Boolean
logToConsole:Boolean
outputs:[
{logOuput:[], logLevel:string}
]
}
Examples
SERVERSIDE - default logLevel should be info
Let's say that we are on the serverside, and we just want to have default logs on production
const logger = new Logger()
logger.info('server is listening on port 3000')
logger.debug(['created new user',userObject])
Now we have problems on the server and we want to also display debug logs.
We can either:
const logger = new logger({logLevel:'debug'})
logger.debug(['created new user',userObject])
CLIENT SIDE - default logLevel should be warn
Let's say that we are in browser environment on staging. We are running a nuxt server, so process.env is available
const logger = new Logger()
logger.info(['Joined room',roomData])
logger.debug(['Sending socket message', message])
logger.warn('this route is deprecated')
Now let's say that we have problems with sending messages throught socket connection.
logger.info(['Joined room', roomData])
logger.debug(['Sending socket message', message])
logger.warn('this route is deprecated')
<3 BadgrHammer, please share any questions or suggestions to me.