Label support
This commit is contained in:
parent
4b6b8e22e8
commit
fc4a4da6cf
@ -66,7 +66,9 @@ type SharedOptionsType = {
|
||||
guard?: string,
|
||||
customStreams?: string[]
|
||||
logLevel?: LogLevel,
|
||||
logLevelMapping?: LogLevelType
|
||||
logLevelMapping?: LogLevelType,
|
||||
customTypes?: string[],
|
||||
labels?: string[],
|
||||
}
|
||||
|
||||
const SharedOptions: SharedOptionsType = {
|
||||
@ -74,12 +76,13 @@ const SharedOptions: SharedOptionsType = {
|
||||
customStreams,
|
||||
logLevel: LogLevel.info,
|
||||
logLevelMapping,
|
||||
customTypes: [],
|
||||
labels: []
|
||||
};
|
||||
|
||||
export type LoggerMasterOptions = SharedOptionsType & {
|
||||
fileRotationFreq?: number,
|
||||
directory?: string,
|
||||
customTypes?: string[],
|
||||
customTypeMapping?: { [key: string]: string },
|
||||
customColours?: { [key: string]: number | string },
|
||||
broadcastLevel?: number,
|
||||
@ -101,9 +104,11 @@ const MasterOptions: LoggerMasterOptions = {
|
||||
skipFileWrite
|
||||
};
|
||||
|
||||
export type LoggerClientOptions = SharedOptionsType;
|
||||
export type LoggerClientOptions = SharedOptionsType & {
|
||||
name?: string
|
||||
};
|
||||
|
||||
const ClientOptions = {
|
||||
const ClientOptions: LoggerClientOptions = {
|
||||
...SharedOptions
|
||||
};
|
||||
|
||||
|
@ -1,23 +1,12 @@
|
||||
// const { inspect } = require('node:util');
|
||||
// const Defaults = require('./Defaults');
|
||||
import Defaults from './Defaults.js';
|
||||
import Defaults, { LoggerClientOptions } from './Defaults.js';
|
||||
import { inspect } from 'node:util';
|
||||
import { LogFunction, WriteOptions } from './Types.js';
|
||||
import { Logger } from './LoggerInterface.js';
|
||||
import { makePlainError } from './Shared.js';
|
||||
|
||||
type ClientOptions = {
|
||||
name?: string,
|
||||
guard?: string,
|
||||
logLevel?: number,
|
||||
customTypes?: string[],
|
||||
logLevelMapping?: {
|
||||
[key: string]: number
|
||||
}
|
||||
}
|
||||
|
||||
type TransportOptions = {
|
||||
type: string
|
||||
type: string,
|
||||
labels?: string[]
|
||||
}
|
||||
|
||||
class LoggerClient implements Logger
|
||||
@ -32,10 +21,10 @@ class LoggerClient implements Logger
|
||||
#_logLevelMapping: { [key: string]: number };
|
||||
#_name: string;
|
||||
#types: string[];
|
||||
#labels: string[];
|
||||
|
||||
constructor (opts: ClientOptions = Defaults.ClientOptions)
|
||||
constructor (opts: LoggerClientOptions = Defaults.ClientOptions)
|
||||
{
|
||||
|
||||
this.#_name = opts.name || opts.constructor.name;
|
||||
if (this.#_name === 'Object')
|
||||
this.#_name = 'unknown';
|
||||
@ -49,17 +38,20 @@ class LoggerClient implements Logger
|
||||
this.#_logLevelMapping = { ...Defaults.ClientOptions.logLevelMapping, ...opts.logLevelMapping };
|
||||
this.#_guard = opts.guard || Defaults.ClientOptions.guard as string;
|
||||
this.#_logLevel = opts.logLevel ?? Defaults.ClientOptions.logLevel as number;
|
||||
this.#labels = opts.labels ?? [];
|
||||
|
||||
for (const type of this.#types)
|
||||
{
|
||||
if (typeof this.#_logLevelMapping[type] === 'undefined')
|
||||
throw new Error(`Missing logLevelMapping for type ${type}`);
|
||||
Object.defineProperty(this, type, {
|
||||
value: (msg: string, o: WriteOptions) => this.#transport(msg, { ...o, type })
|
||||
value: (msg: string, o: WriteOptions) =>
|
||||
{
|
||||
const { labels = [], ...writeOpts } = o;
|
||||
this.#transport(msg, { ...writeOpts, type, labels: [ ...this.#labels, ...labels ] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
get logLevel ()
|
||||
|
@ -52,13 +52,14 @@ class MasterLogger implements Logger
|
||||
#pruneInterval: NodeJS.Timer;
|
||||
#pruneDays: number;
|
||||
#skipFileWrite: boolean;
|
||||
#labels: string[];
|
||||
|
||||
constructor (config = Defaults.MasterOptions)
|
||||
{
|
||||
const {
|
||||
directory, customTypes = [], customStreams = [], customTypeMapping,
|
||||
customColours, guard, fileRotationFreq, logLevel, logLevelMapping,
|
||||
webhook, broadcastLevel, pruneDays, skipFileWrite
|
||||
webhook, broadcastLevel, pruneDays, skipFileWrite, labels = []
|
||||
} = { ...Defaults.MasterOptions, ...config };
|
||||
|
||||
if (!directory)
|
||||
@ -106,6 +107,7 @@ class MasterLogger implements Logger
|
||||
return prev;
|
||||
}, {} as FuncsType);
|
||||
|
||||
this.#labels = labels;
|
||||
this.#streamTypes = [ ...customStreams, 'error', 'default' ];
|
||||
this.#streamTypeMapping = { ...Defaults.TypeStream, ...customTypeMapping };
|
||||
if (this.#skipFileWrite)
|
||||
@ -197,15 +199,15 @@ class MasterLogger implements Logger
|
||||
{
|
||||
if (!msg[this.#_guard])
|
||||
return;
|
||||
const { message, type, header, broadcast } = msg;
|
||||
const { message, type, header, broadcast, labels } = msg;
|
||||
const func = this[type] as LogFunction;
|
||||
if (!func)
|
||||
throw new Error(`Attempted use of invalid logging function of type: ${type}, ensure client and master have the same type definitions.`);
|
||||
func(message, { subheader: header, shard, broadcast });
|
||||
func(message, { subheader: header, shard, broadcast, labels });
|
||||
});
|
||||
}
|
||||
|
||||
write (type = 'info', text: string | object | Error, { subheader = '', shard, broadcast = false }: WriteOptions = {})
|
||||
write (type = 'info', text: string | object | Error, { subheader = '', shard, broadcast = false, labels = [] }: WriteOptions = {})
|
||||
{
|
||||
let colour = this.#colourFuncs[type];
|
||||
if (!colour)
|
||||
@ -231,14 +233,14 @@ class MasterLogger implements Logger
|
||||
}
|
||||
if ((broadcast || (this.#_broadcastLevel <= this.#_logLevelMapping[type])) && this.#webhook)
|
||||
{
|
||||
const description = (subheader.length ? `**${subheader}**\n` : '') + `\`\`\`${text}\`\`\``;
|
||||
const description = (subheader.length ? `**${subheader}**: ${process.env.NODE_ENV ?? 'production'}\n` : '') + `\`\`\`${text}\`\`\``;
|
||||
this.#webhook.send({
|
||||
embeds: [{
|
||||
title: `[__${type.toUpperCase()}__] ${this._shard(shard)}`,
|
||||
description,
|
||||
color: colour.int,
|
||||
footer: {
|
||||
text: `ENV: ${process.env.NODE_ENV ?? 'production'}`
|
||||
text: [ ...labels, ...this.#labels ].join(', ') ?? ''
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
@ -7,16 +7,18 @@ type Shard = {
|
||||
type WriteOptions = {
|
||||
subheader?: string,
|
||||
shard?: Shard,
|
||||
broadcast?: boolean
|
||||
broadcast?: boolean,
|
||||
labels?: string[]
|
||||
}
|
||||
|
||||
type IPCMessage = {
|
||||
[key: string]: string | boolean
|
||||
[key: string]: string | string[] | boolean
|
||||
_guard: string
|
||||
type: string,
|
||||
message: string,
|
||||
header: string,
|
||||
broadcast: boolean
|
||||
broadcast: boolean,
|
||||
labels: string[]
|
||||
}
|
||||
|
||||
type LogFunction = (str: string, opts?: WriteOptions) => void
|
||||
|
Loading…
Reference in New Issue
Block a user