Skip to content

Commit

Permalink
refactor: Replace winston with @foxxmd/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Apr 3, 2024
1 parent d0e4874 commit 1ba48cc
Show file tree
Hide file tree
Showing 18 changed files with 813 additions and 1,309 deletions.
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
},
"dependencies": {
"@awaitjs/express": "^0.6.3",
"@foxxmd/logging": "^0.2.0",
"@foxxmd/redact-string": "^0.1.2",
"@foxxmd/winston": "^3.3.31",
"@supercharge/promise-pool": "^3.0.0",
"ajv": "^7.2.4",
"body-parser": "^1.19.0",
Expand All @@ -50,12 +50,7 @@
"sequelize": "^6.32.1",
"sqlite3": "^5.1.6",
"toad-scheduler": "^3.0.0",
"triple-beam": "^1.3.0",
"umzug": "^3.2.1",
"winston-daily-rotate-file": "^4.5.0",
"winston-duplex": "0.1.3",
"winston-null": "^2.0.0",
"winston-transport": "^4.4.0",
"yaml": "^2.3.1"
},
"devDependencies": {
Expand All @@ -68,7 +63,6 @@
"@types/formidable": "^2.0.5",
"@types/mocha": "^9.1.0",
"@types/node": "^18.0.0",
"@types/triple-beam": "^1.3.2",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"lorem-ipsum": "^2.0.8",
Expand Down
8 changes: 4 additions & 4 deletions src/api/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {addAsync, Router} from '@awaitjs/express';
import { childLogger, Logger } from "@foxxmd/logging";
import express from 'express';
import {OperatorConfig} from "../common/infrastructure/OperatorConfig.js";
import {AppLogger} from "../common/logging.js";
import {mergeArr} from "../utils/index.js";
import {tautulliFormMiddleware} from "./tautulliFormMiddleware.js";
import {IncomingFileData} from "../common/infrastructure/Atomic.js";
Expand All @@ -19,10 +19,10 @@ let envPort = process.env.PORT ?? 8078;

app.use(router);

export const initServer = async (config: OperatorConfig, parentLogger: AppLogger) => {
export const initServer = async (config: OperatorConfig, parentLogger: Logger) => {

const apiLogger = parentLogger.child({labels: ['API']}, mergeArr);
const ingressLogger = apiLogger.child({labels: ['Tautulli Request']}, mergeArr);
const apiLogger = childLogger(parentLogger, 'API');
const ingressLogger = childLogger(parentLogger, 'Tautulli Request');
try {
const {
port = envPort
Expand Down
6 changes: 3 additions & 3 deletions src/api/tautulliFormMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AppLogger} from "../common/logging.js";
import { childLogger, Logger } from "@foxxmd/logging";
import {mergeArr} from "../utils/index.js";
import formidable, {File, Files, VolatileFile} from "formidable";
import concatStream from 'concat-stream';
Expand All @@ -17,8 +17,8 @@ const fileIdentifier = (name: string, file: any) => {
return identifier;
}

export const tautulliFormMiddleware = (parentLogger: AppLogger) => {
const logger = parentLogger.child({labels: ['Form']}, mergeArr);
export const tautulliFormMiddleware = (parentLogger: Logger) => {
const logger = childLogger(parentLogger, 'Form');

return async (req: any, res: any, next: any) => {

Expand Down
9 changes: 3 additions & 6 deletions src/common/config/ConfigBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import winston, {Logger} from "@foxxmd/winston";
import path from "path";
import { Logger, loggerDebug, LogLevel } from "@foxxmd/logging";
import {dataDir, projectDir} from "../index.js";
import {readFile, readFileToString} from "../../utils/io.js";
import {ErrorWithCause} from "pony-cause";
Expand All @@ -16,11 +15,9 @@ import {createAjvFactory} from "../../utils/validation.js";
import {Schema} from "ajv";
import * as operatorSchema from '../schema/operator.json';
import merge from 'deepmerge';
import {LogLevel} from "../infrastructure/Atomic.js";
import {overwriteMerge} from "../../utils/index.js";
import {getLogger, AppLogger} from "../logging.js";

export const validateJson = <T>(config: object, schema: Schema, logger: AppLogger): T => {
export const validateJson = <T>(config: object, schema: Schema, logger: Logger): T => {
const ajv = createAjvFactory(logger);
const valid = ajv.validate(schema, config);
if (valid) {
Expand Down Expand Up @@ -73,7 +70,7 @@ export const validateJson = <T>(config: object, schema: Schema, logger: AppLogge

export const parseConfigFromSources = async () => {

const initLogger = winston.loggers.get('init') as AppLogger;
const initLogger = loggerDebug;

let configDoc: YamlOperatorConfigDocument
let configFromFile: OperatorJsonConfig = {digests: []};
Expand Down
12 changes: 5 additions & 7 deletions src/common/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getLogger} from "../logging.js";
import { childLogger, Logger } from "@foxxmd/logging";
import path from "path";
import {dataDir, projectDir} from "../index.js";
import {Sequelize} from "sequelize";
Expand All @@ -9,8 +9,8 @@ import {setupMappings} from "./setup.js";
import {Options} from "sequelize/types/sequelize.js";
import {redactString} from "@foxxmd/redact-string";

export const initDB = async (config: OperatorConfig) => {
const logger = getLogger(config.logging, 'DB');
export const initDB = async (config: OperatorConfig, parentLogger: Logger) => {
const logger = childLogger(parentLogger, 'DB');

const {
logging: {
Expand Down Expand Up @@ -60,7 +60,7 @@ export const initDB = async (config: OperatorConfig) => {

setupMappings(sequelize);

await runMigrations(sequelize);
await runMigrations(sequelize, logger);

return sequelize;
}
Expand All @@ -76,9 +76,7 @@ const logFunc = (payload: Record<string, unknown>) => {
return parts.join('');
}

const runMigrations = async (db: Sequelize): Promise<void> => {

const logger = getLogger(undefined, 'DB');
const runMigrations = async (db: Sequelize, logger: Logger): Promise<void> => {

//const logFunc = umzugLoggerFunc(logger);

Expand Down
8 changes: 4 additions & 4 deletions src/common/funcs/processPendingDigests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { childLogger, Logger } from "@foxxmd/logging";
import {DigestData} from "../infrastructure/OperatorConfig.js";
import {AppLogger} from "../logging.js";
import {mergeArr, sleep} from "../../utils/index.js";
import {WebhookClient} from "discord.js";
import {TautulliRequest} from "../db/models/TautulliRequest.js";
import {buildMessages} from "../../discord/builder.js";
import {ErrorWithCause} from "pony-cause";

export const processPendingDigests = async (digest: DigestData, parentLogger: AppLogger, id?: string) => {
const digestLogger = parentLogger.child({labels: [`Digest ${id ?? digest.slug}`]}, mergeArr);
export const processPendingDigests = async (digest: DigestData, parentLogger: Logger, id?: string) => {
const digestLogger = childLogger(parentLogger, `Digest ${id ?? digest.slug}`);

const {
slug,
Expand All @@ -16,7 +16,7 @@ export const processPendingDigests = async (digest: DigestData, parentLogger: Ap
}
} = digest;

const logger = digestLogger.child({labels: slug}, mergeArr);
const logger = childLogger(digestLogger, slug);

let sentEvents = 0;
let sentMessages = 0;
Expand Down
63 changes: 1 addition & 62 deletions src/common/infrastructure/Atomic.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
import {MESSAGE} from 'triple-beam';
import {BaseMessageOptions} from "discord.js";

export type LogLevel = "error" | "warn" | "safety" | "info" | "verbose" | "debug";
export const logLevels = ['error', 'warn', 'info', 'verbose', 'debug'];
export type DiscordLogLevel = "error" | "warn" | "safety" | "info" | "verbose" | "debug";

export type ConfigFormat = 'yaml';

export interface LogConfig {
level?: string
file?: string | false
stream?: string
console?: string
db?: boolean
}

export interface LogOptions {
/**
* Specify the minimum log level for all log outputs without their own level specified.
*
* Defaults to env `LOG_LEVEL` or `info` if not specified.
*
* @default 'info'
* */
level?: LogLevel
/**
* Specify the minimum log level to output to rotating files. If `false` no log files will be created.
* */
file?: LogLevel | false
/**
* Specify the minimum log level streamed to the UI
* */
stream?: LogLevel
/**
* Specify the minimum log level streamed to the console (or docker container)
* */
console?: LogLevel

db?: boolean
}

export const asLogOptions = (obj: LogConfig = {}): obj is LogOptions => {
return Object.entries(obj).every(([key, val]) => {
if(key === 'db') {
return val;
}
if(key !== 'file') {
return val === undefined || logLevels.includes(val.toLocaleLowerCase());
}
return val === undefined || val === false || logLevels.includes(val.toLocaleLowerCase());
});
}

export interface LogInfo extends LogInfoMeta {
message: string
[MESSAGE]: string,
level: string
timestamp: string
transport?: string[]
stack?: string
}

export interface LogInfoMeta {
labels?: string[]
[key: string]: any
}

export interface NamedGroup {
[name: string]: any
}
Expand Down
28 changes: 4 additions & 24 deletions src/common/infrastructure/Logging.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import {LogLevel} from "./Atomic.js";

export interface LoggingOptions {
/**
* Specify the minimum log level for all log outputs without their own level specified.
*
* Defaults to env `LOG_LEVEL` or `info` if not specified.
*
* @default 'info'
* */
level?: LogLevel
/**
* Specify the minimum log level to output to rotating files. If `false` no log files will be created.
* */
file?: LogLevel | false
/**
* Specify the minimum log level streamed to the UI
* */
stream?: LogLevel
/**
* Specify the minimum log level streamed to the console (or docker container)
* */
console?: LogLevel
import { LogOptions } from "@foxxmd/logging";
import { DiscordLogLevel } from "./Atomic.js";

export interface LoggingOptions extends LogOptions {
db?: boolean

discord?: LogLevel
discord?: DiscordLogLevel
}
Loading

0 comments on commit 1ba48cc

Please sign in to comment.