1.0.3 • Published 1 year ago

front-logs v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Front Logs

Frontend log engine with ability to send logs to server, auto retry when error occurs, use indexedDB to cache logs and auto resend

Startup

npm install --save front-logs

To develop, require Node >= 14

Usage

Case

import { LogEngine, LogBean, LogRequest, LogHandler } from 'front-logs'

const engine = new LogEngine() // start log engine

const request = new LogRequest({
  baseDomain: 'http://localhost:3000',
  url: '/push/logs',
  params: { logs: [] }
}) // init log request
engine.setLogRequest(request) // register request as a default request of engine

const handler = new LogHandler()
handler.connect() // connect to log engine

if (!handler.logEngine) return // if connection failed, means engine does not exist and log will not be sent

const log = new LogBean('This is a warning', 'WARN')
handler.appendLog(log) // log
handler.sendLog() // call engine to send log to server

engine.destroy() // call destroy if engine is not used any more for preventing memory leaks

LogEngine

constructor(target: string = '$XFrontLogsEngine') {}

Log engine controls log to send, auto retry, cached using indexedDB and resend

target: indicates where log engine mounts itself, defaults to window.$XFrontLogsEngine

MethodArgumentsReturnValueDescription
appendLogLogBean, custom: booleanvoidAppend a log to engine, if auto send is not initialed, auto send will start. The second parameter indicates if this log is a custom log
setSendIntervalinterval: number(s)LogEnginedefaults to 300(s, 5 minutes)
sendLogRequestvoidSend log immediately if engine is available
setLogRequestLogRequestLogEngineSet default request of an engine
destroyvoidvoidDestroy an engine instance and cancel all event listeners

LogHandler

constructor() {}

Log handler proxies log actions to append and send logs wherever

MethodArgumentsReturnValueDescription
connectengineTargetLogHandlerConnect to log engine, any first time you want to use log handler, you should call this method. The engineTarget indicates where to find a log engine instance
appendLogmessage: string, type: stringLogHandlerAppend a log to engine
appendCustomLogLogBeanLogHandlerAppend a custom log to engine
sendLogvoidLogHandlerCall engine to send log immediately
setRequestLogRequestLogHandlerSet request of handler, if specified, every time use this handler to send log will use this request

LogBean

constructor(type?: string, message?: string) {}

Class for bearing log content. Auto generate log id and timestamp

MethodArgumentsReturnValueDescription
toJSONvoidobjectIndicates how to transform log into JSON. Defaults to transform log type and message

For custom log, create a class extends LogBean and overwritten toJSON()

Note: Needn't transform log id and timestamp those are sent automatically

LogRequest

constructor({ baseDomain, url, params }: { baseDomain?: string, url?: string, params?: any }) {}

Log request specifies data while send request

MethodArgumentsReturnValueDescription
setRequest{baseDomain?: string, url?: string, params?: any }LogRequestSet every parameter of request
getFullUrlvoidstringReturn full url of a request. Equals to baseDomain + url
getDatavoidanyReturn data of request. Data will merge data of globalArguments
setGlobalArguments{ baseDomain, globalData }voidSee detailed Information below

GlobalArguments

Most cases, there are some public parameters that every request should carry them such as xdaptoken

The reason this project don't use headers for carrying them is for compatible of sendBeacon() which does not support custom request headers

Turn to use setGlobalArguments to set them. For example:

LogRequest.setGlobalArguments({ baseDomain, globalData })

Global Arguments only effects those log request instances constructed after called setGlobalArguments()

Simple Demo

npm run build:demo

Run code above to generate browser.js under public directory and run a web server under this project to access public/index.html

Suggest Live Server which is an extension of VS code editor. In this case, access http://127.0.0.1:5500/public/index.html to see a very simple demo

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago