diff --git a/pending-packages/plugin-instance-logger-file/.npmignore b/pending-packages/plugin-instance-logger-file/.npmignore deleted file mode 100644 index d1589c83..00000000 --- a/pending-packages/plugin-instance-logger-file/.npmignore +++ /dev/null @@ -1,5 +0,0 @@ -* -!**/*.ts -!package.json -!readme.md -!LICENSE.md \ No newline at end of file diff --git a/pending-packages/plugin-instance-logger-file/package.json b/pending-packages/plugin-instance-logger-file/package.json deleted file mode 100644 index b4874569..00000000 --- a/pending-packages/plugin-instance-logger-file/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "@pockethost/plugin-file-instance-logger", - "version": "0.0.1", - "description": "", - "main": "src/index.ts", - "module": "src/index.ts", - "types": "src/index.ts", - "type": "module", - "scripts": { - "check-types": "tsc --noEmit " - }, - "keywords": [], - "author": "", - "license": "ISC", - "devDependencies": { - "@types/tail": "^2.2.2", - "tail": "^2.2.6", - "type-fest": "^4.6.0", - "typescript": "^5.4.5", - "winston": "^3.11.0" - }, - "dependencies": { - "pockethost": "workspace:^" - } -} diff --git a/pending-packages/plugin-instance-logger-file/src/InstanceLoggerService/index.ts b/pending-packages/plugin-instance-logger-file/src/InstanceLoggerService/index.ts deleted file mode 100644 index 30e13825..00000000 --- a/pending-packages/plugin-instance-logger-file/src/InstanceLoggerService/index.ts +++ /dev/null @@ -1,176 +0,0 @@ -import * as fs from 'fs' -import { - LoggerService, - asyncExitHook, - createCleanupManager, - mergeConfig, - mkInstanceDataPath, - stringify, -} from 'pockethost/core' -import { Tail } from 'tail' -import * as winston from 'winston' - -type UnsubFunc = () => void - -export type InstanceLoggerApi = { - info: (msg: string) => void - error: (msg: string) => void - tail: (linesBack: number, data: (line: winston.LogEntry) => void) => UnsubFunc - shutdown: () => void -} - -export type InstanceLoggerOptions = { - ttl: number -} - -const loggers: { - [key: string]: InstanceLoggerApi -} = {} - -export function InstanceLogger( - instanceId: string, - target: string, - options: Partial = {}, -) { - const { dbg, info } = LoggerService().create(instanceId).breadcrumb(target) - const { ttl } = mergeConfig({ ttl: 0 }, options) - - dbg({ ttl }) - - const loggerKey = `${instanceId}_${target}` - if (loggers[loggerKey]) { - dbg(`Logger exists, using cache`) - return loggers[loggerKey]! - } - - const logDirectory = mkInstanceDataPath(instanceId, `logs`) - if (!fs.existsSync(logDirectory)) { - dbg(`Creating ${logDirectory}`) - fs.mkdirSync(logDirectory, { recursive: true }) - } - - const logFile = mkInstanceDataPath(instanceId, `logs`, `${target}.log`) - - const cm = createCleanupManager() - - const fileTransport = new winston.transports.File({ - filename: logFile, - maxsize: 100 * 1024 * 1024, // 100MB - maxFiles: 10, - tailable: true, - zippedArchive: true, - }) - const logger = winston.createLogger({ - format: winston.format.combine( - winston.format.timestamp(), - winston.format.json(), - winston.format.printf((info) => { - return stringify({ - stream: info.level === 'info' ? 'stdout' : 'stderr', - time: info.timestamp, - message: info.message, - }) - }), - ), - transports: [fileTransport], - }) - - cm.add(() => { - dbg(`Deleting and closing`) - delete loggers[loggerKey] - fileTransport.close?.() - logger.close() - }) - - const { error, warn } = LoggerService() - .create('InstanceLogger') - .breadcrumb(instanceId) - .breadcrumb(target) - - const resetTtl = (() => { - let tid: ReturnType - - return () => { - if (!ttl) return - clearTimeout(tid) - tid = setTimeout(() => { - dbg(`Logger timeout`) - api.shutdown() - }, ttl) - } - })() - - const api = { - info: (msg: string) => { - resetTtl() - dbg(`info: `, msg) - logger.info(msg) - }, - error: (msg: string) => { - resetTtl() - dbg(`error: `, msg) - logger.error(msg) - }, - tail: ( - linesBack: number, - data: (line: winston.LogEntry) => void, - ): UnsubFunc => { - if (ttl) { - throw new Error(`Cannot tail with ttl active`) - } - const logFile = mkInstanceDataPath(instanceId, `logs`, `${target}.log`) - - let tid: any - cm.add(() => clearTimeout(tid)) - const check = () => { - try { - const tail = new Tail(logFile, { nLines: linesBack }) - - tail.on('line', (line) => { - try { - const entry = JSON.parse(line) - data(entry) - } catch (e) { - data({ - level: 'info', - message: line, - time: new Date().toISOString(), - }) - } - }) - tail.on('error', (e) => { - error(`Caught a tail error ${e}`) - }) - - cm.add(() => tail.unwatch()) - } catch (e) { - warn(e) - tid = setTimeout(check, 1000) - } - } - check() - - const unsub = asyncExitHook(() => cm.shutdown()) - - return () => { - cm.shutdown() - unsub() - } - }, - shutdown: () => cm.shutdown(), - } - - loggers[loggerKey] = api - return api -} - -// // Example usage -// const loggerInstance = InstanceLogger('123', 'my-target') -// loggerInstance.info('This is an info message') -// loggerInstance.error('This is an error message') -// const unsubscribe = loggerInstance.tail(10, (line) => { -// console.log(line) -// }) - -// // Later when you want to stop listening to the tail: -// // unsubscribe(); diff --git a/pending-packages/plugin-instance-logger-file/src/index.ts b/pending-packages/plugin-instance-logger-file/src/index.ts deleted file mode 100644 index e67d0344..00000000 --- a/pending-packages/plugin-instance-logger-file/src/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { join } from 'path' -import { - LoggerService, - PH_HOME, - PocketHostPlugin, - Settings, - mkPath, -} from 'pockethost/core' - -const _HOME_DIR = - process.env.PH_SIL_HOME || join(PH_HOME(), `plugin-file-instance-logger`) - -const settings = Settings({ - PH_FIL_HOME: mkPath(_HOME_DIR, { create: true }), -}) - -const logger = LoggerService().create('plugin-file-instance-logger') -export const { dbg } = logger - -const plugin: PocketHostPlugin = async ({ registerAction, registerFilter }) => { - dbg(`initializing plugin-file-instance-logger`) -} - -export default plugin diff --git a/pending-packages/plugin-instance-logger-file/tsconfig.json b/pending-packages/plugin-instance-logger-file/tsconfig.json deleted file mode 100644 index d43b5981..00000000 --- a/pending-packages/plugin-instance-logger-file/tsconfig.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "compilerOptions": { - "lib": ["ES2021.String"], - "allowJs": true, - "checkJs": true, - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "resolveJsonModule": true, - "skipLibCheck": true, - "sourceMap": true, - "strict": true, - "target": "ESNext", - "module": "ESNext", - "moduleResolution": "node", - "noUncheckedIndexedAccess": true, - "strictNullChecks": true, - "noEmit": true - }, - "include": [ - "**/*.ts", - "../plugin-instance-logger-file-realtime-tail/src/RealtimeLog.ts" - ] -}