🚀 A high-performance, high-capacity, and highly scalable web-side logging system based on WebWorker and indexedDB
Online use | 中文 | Changelog | issue feedback | Gitee
0. Introduction
idb-logger is committed to helping web developers access a high-performance logging system conveniently and efficiently. Relying on webworker and indexedDB technologies, web applications can store large-capacity logs in the browser in a way that hardly affects the user experience, and Report it to the server at the right time, or download it by the user.
0.1 Features
- Support WebWorker + indexedDB to store logs
- Three log storage modes can be selected, and when not supported, the supported mode will be automatically selected down
- Support no storage mode, only use idb-logger as a log generation tool
- Support to define the maximum number of storage logs, the oldest records will be automatically deleted
- Support download log
- Support query log, support multiple query modes
- Support custom basic data
- Support custom reporting based on onReport callback
1. Quick use
1.0 install
1.0.1 npm install
npm i idb-logger
import Logger from 'idb-logger';
1.0.2 cdn
<script src="https://cdn.jsdelivr.net/npm/idb-logger/idb-logger.min.js"></script>
<script>
window.IDBLogger;
</script>
1.1 Quick use
const logger = new Logger();
logger.log('an ordinary log');
logger.error('an error log', 'additional information', {type: 'additional information'});
logger.download();
1.2. API introduction
ts declaration:
declare class Logger {
static version: string;
private_store;
id: string;
storeType: TLogStoreType;
constructor({ id, useConsole, storeType, maxRecords, baseInfo, onReport, onDiscard, }?: ILoggerOption);
injectBaseInfo(baseInfo?: IBaseInfoOption & IJson): Promise<void>;
log(...args: any[]): Promise<IAddReturn>;
error(...args: any[]): Promise<IAddReturn>;
warn(...args: any[]): Promise<IAddReturn>;
info(...args: any[]): Promise<IAddReturn>;
private _logCommon;
close(): Promise<boolean>;
destory(): Promise<boolean>;
clear(): Promise<boolean>;
count(): Promise<number>;
delete(logid: string): Promise<boolean>;
refreshTraceId(): Promise<void>;
refreshDurationStart(): Promise<void>;
download({ name, filter }?: {
name?: string;
filter?: TFilterOption;
keys?: string[];
}): Promise<number>;
get(logid: string): Promise<ILogDBData | null>;
getAll(): Promise<ILogDBData[]>;
filter(filter?: TFilterOption): Promise<ILogDBData[]>;
}
complete logdata
interface ILogDBData {
uid: string;
clientid: string;
traceid: string;
network: string;
url: string;
ua: string;
msg: string;
payload?: any;
type: TLogType;
duration: number;
time: string;
timestamp: number;
logid: string;
}
1.2.1 Constructor
new Logger({
id,
useConsole,
storeType,
maxRecords,
baseInfo,
onReport,
onDiscard
});
idb means using indexedDB to store logs, storage means using localStorage to store logs, temp means using js variables to store data (non-persistent), none means not storing data (only using idb-logger as a log generation tool)
The default value is idb. When a certain storage type is not supported by the browser, the next mode will be automatically selected backward.
In order to reduce client performance consumption, you can specify a maximum storage amount. When this amount is exceeded, the logger will automatically delete the oldest record and trigger the onDiscard callback
Accepts a json, which is used to inject the basic information of the log. When the name is consistent with the default baseInfo, the default baseInfo will be overwritten
Default baseInfo clientid, uid, traceid, network, url, ua
1.2.2 Logging
There are four methods on the logger object: log, error, warn, info
The usage method is similar to support the incoming of any amount and any type of data
parameter rules
- When the first parameter is a string or a number, the parameter is used as the msg field of the log, and all the following parameters are combined into an array as the payload attribute
- When the first parameter is json and there is only one parameter, all the properties in the json will be overwritten to the properties of the log
- When the first parameter is not a number or a string, the default value def will be used as msg, and all parameters will be combined into an array as the payload attribute
await logger.log('start');
await logger.info({
msg: 'start',
time: 'xxxx',
your_custom: 'xxxx',
});
await logger.warn({}, [], '', 1);
await logger.error('error', {}, [], '', 1);
1.2.3 Query
await logger.filter(filter);
filter supports three modes
Support to pass in a function (data: ILogDBData) => boolean
, the callback function is the log data, return true or false to indicate whether the data hits
Note: In indexedDB mode, since the function will be passed to the worker for execution, it is required that other methods or features not supported in the worker cannot be called in the function.
await logger.filter(item=>{
return item.msg.includes('xxx') && item.type === 'log';
})
Pass in a json, each attribute will be used and String together, supports regular expressions
await logger.filter({
msg: /xxx/i,
type: 'log',
})
Pass in a json array, each element in the array is connected with or logic, and the and logic is used between the attributes within the element, which is consistent with 2
await logger.filter([{
msg: /xxx/i,
}, {
type: 'log',
}])
The above statement means that msg in the log contains xxx or type is equal to log
1.2.4 Download
Download logs stored in indexedDB
await logger.download({
name,
filter,
keys,
});
1.2.5 Other APIs
1.2.5.1 getAll
get all logs
await logger.getAll();
1.2.5.1 get
Get a log based on the log id
await logger.getAll(logid);
1.2.5.1 count
Get the number of logs
await logger.count();
1.2.5.1 delete
Delete a log by log id
await logger.delete(logid);
1.2.5.1 injectBaseInfo
Inject log basic information
await logger.injectBaseInfo({
network: 'wifi',
phone: 'xxxx',
});
1.2.5.1 refreshTraceId
Refresh the traceid, generally used for reconnection, etc., considered to be the second visit scenario
Also refreshDurationStart
await logger.refreshTraceId();
1.2.5.1 refreshDurationStart
Refresh the timing starting point, generally used in scenarios that need to re-count the time
await logger.refreshTraceId();
1.2.5.1 close
close the database
await logger.close();
1.2.5.1 clear
close and clear the database
await logger.clear();
1.2.5.1 destory
Close, empty and delete the database
await logger.destory();