dimoba-log
v1.0.8
Published
A module that contains log models and method
Downloads
2
Readme
dimoba-log
A module in TypeScript for structuring logs.
Getting started
Installing
npm install dimoba-log --save
Usage
import { Log, LogRequest, LogLevel, LogQueue, LogQueueConfig } from 'dimoba-log';
Log model uses a simple syntax to define a log:
class Log {
level: number; // See LogLevel model below for further detail
date: number; // In millisecond
message: string; // Log message itself
}
LogLevel enum defines an attributed number for each log level:
enum class LogLevel {
Debug = 0,
Info,
Warn,
Error
}
LogRequest model contains a list of Log generated as well as few information about the user who sent his error report:
class LogRequest {
appVersion: string; // App version
token: string; // User token
issues?: string[]; // A list of issue selected by user
otherIssue?: string; // Another issue specified by user
logs: Log[]; // Array of Log
}
LogQueue
A class which manages a log queue.
class LogQueue {
// Takes a Log as parameter and adds it in Log queue.
add(log: Log)
// Stringify and returns a Log queue where each entry has its log level converted in string to be more readable
toString(): string
static filter(logs: Log[]): Log[]
static sort(logs: Logs): Log[]
}
Define a LogQueueConfig in the constructor to set the limit of queue.
class LogQueueConfig {
maxLines: number
maxCharactersPerLine: number
}
const logQueue = new LogQueue(new LogQueueConfig({maxLines: 500, maxCharactersPerLine: 400}))
Any values not specified will be set to the defaults below:
{
maxLines: 10000,
maxCharactersPerLine: 1000
}
Static methods
LogQueue has two static methods that could be used to manage your own Log array:
LogQueue.filter(logs: Log[]) method takes an array of Log as parameter and checks if it contains malformed entries. An error is thrown if any element is null or its date is null or undefined
try {
logs = LogQueue.filter(logs)
} catch (e) {
// Catch your error
}
LogQueue.sort(logs: Log[]) method takes an array of Log as parameter and sort it by date.
try {
logs = LogQueue.sort(logs)
} catch (e) {
// Catch your error
}
N.B.: sort() method calls filter() first before to check the malformed entries
Author
Dimoba