Skip to content

Instantly share code, notes, and snippets.

@scazzy
Last active November 15, 2018 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scazzy/eea78e1eecadc5dcf02b2c62c019e1eb to your computer and use it in GitHub Desktop.
Save scazzy/eea78e1eecadc5dcf02b2c62c019e1eb to your computer and use it in GitHub Desktop.
Logger system basic boilerplate
// api - req/res, duration, failures+count+reqId
// action - user actions or general report
// perf - durations
// error - all errors, or custom (similar to capturing in sentry)
class Logger {
static type = {
api: {
name: '',
rules: {},
// ...
},
action: {
name: '',
rules: {},
// ...
},
}
static PERSIST_INTERVAL = 30000
constructor (props = {}) {
// in-memory collection of logs type-wise
// these logs would be periodically synced with storage and flushed in-memory
this._accumulatedLog = {
// api: [{Log, Log, ...}],
// action: [{Log, Log, ...}],
}
this.__processLogs = throttle(this._processLogs, Logger.PERSIST_INTERVAL)
if (props && Object.keys(props).length) {
this.props = props;
}
}
// accumulate the logs in
// priority logs can immediately sync or report if required
_addLog = (type, log:Log, options = {}, priority, ...) => {
if(!this._accumulatedLog[type]) this._accumulatedLog[type] = {}
// ...
this._accumulatedLog[type].push(log)
// ...
this.__processLogs()
}
_processLogs = ({immediate = false}) => {
const clonedLogs = {...this._accumulatedLog} // or something faster than spread
this._accumulatedLog = {} // either here or once persis
this._persistLogs(clonedLogs)
}
// write all or specific type log to the relevant storage
_persistLogs = (logsToPersist) => {
// write logic to idb, s3, or file system etc
// this._accumulatedLog[type] = {}
if(this.props.persistLogs) {
// can be overwritten for desktop
this.props.persistLogs.call(this, logsToPersist)
return false;
}
// ... default
}
// sync specific or all logs with s3 or required server service for debug or remote access
_syncLogs = () => {
if(this.props.syncLogs) {
// can be overwritten for desktop
this.props.syncLogs.call(this)
return false;
}
// ... default
}
// flush logs (periodic, specific type, or all) from storage on need/logic-basis
_flush = () => {
// or purge
}
////
//// PUBLIC METHODS
////
// log.api()
api = () => {this._addLog('api', ...)}
// log.action()
action = () => {this._addLog('action', ...)}
// log.perf()
perf = () => {this._addLog('perf', ...)}
}
export default new Logger()
// Desktop - extend a new class
class DesktopLogger extends Logger {
static PERSIST_INTERVAL = 120000
constructor () {
super()
}
_persistLogs = () => {
// fs based storage
}
_syncLogs = () => {
// if need an overwrite
}
}
// Log - model
class Log {
type: 'api|action|perf', // index
timestamp: Date().now(), // index
name: 'some-event-name', // index,
objectId: null, // index, if object id available for faster search
data: {},
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment