This is a logger lib written by vanilla JS. You just need config it with some code, and it will provide you with reliable, automatic, intelligent function.
- setMode
- setLogLevel
- setSource
- setVersion
- setBatchSize
- setBatchTimeout
- setPersistence
- setPersistenceEngine
- setStoragePrefix
- onFormat
- onSend
mode
(Enum
)Server
Once logs prepare for sending, will call theonSend
Console
Only print logs on the console, but not callonSend
level
(Enum
)All
print logs of any levelDebug
Info
Notice
Warning
Error
Critical
Alert
Emergency
Off
print none
source
(Enum
)Web
WebMobile
WebWeChat
App
version
(String
)
Optional version info.
size
(Number
)
If number of logs in buffer reached the size, will call onSend
.
time
(Number
)
If time interval reached, will call onSend
.
isPersistence
(Boolean
)
Whether or not persistence function is open.
engine
(PersistenceEngine
)LocalStorage
built-inSessionStorage
built-in
Persists logs in storage. Certainly, you can provide engine yourself.
prefix
(String
)
Key prefix.
onFormatCallbak
(Function
)
You can define the callbak to format your log.
Arguments:
context
(Object
) Contains some info such as version, source, time, level.
contents
(Array
) All contents printed by calling Logger.xxx().
onSendCallbak
(Function
)
You can define the callbak to send your logs.
Arguments:
logs
(Array
) The logs will be sent to server.
cb
(Function
) If you finish sending logs, remembering to call it.
See index.html.
Logger
.setMode(Logger.Mode.Server)
.setLogLevel(Logger.Level.Debug)
.setSource(Logger.Source.Web)
.setVersion('2.0.0')
.setBatchSize(10)
.setBatchTimeout(10000)
.setPersistence(true)
.setPersistenceEngine(Logger.PersistenceEngine.LocalStorage)
.setStoragePrefix('log_')
.onFormat((ctx, contents) => {
return {
level: ctx.level,
time: ctx.time,
message: contents,
source: ctx.source,
version: ctx.version
}
})
.onSend((logs, cb) => {
console.log(logs)
cb()
})
MIT