Skip to content

Commit

Permalink
Log streams (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez authored Nov 25, 2024
1 parent b62b407 commit e7f2a63
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 181 deletions.
16 changes: 1 addition & 15 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,7 @@ export default [
"@typescript-eslint/unbound-method": "error",
"import/no-extraneous-dependencies": ["error", { packageDir: "./" }],
"sonarjs/prefer-immediate-return": "off",
"unicorn/prevent-abbreviations": [
"error",
{
replacements: {
docs: false,
e: false,
dir: false,
i: false,
params: false,
props: false,
ref: false,
temp: false,
},
},
],
"unicorn/prevent-abbreviations": "off",
"no-case-declarations": "off",
"no-async-promise-executor": "off",
"unicorn/prefer-node-protocol": "off",
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": {
"url": "git+https://github.com/Digital-Alchemy-TS/core"
},
"version": "24.11.3",
"version": "24.11.4",
"author": {
"url": "https://github.com/zoe-codez",
"name": "Zoe Codez"
Expand Down Expand Up @@ -66,7 +66,7 @@
"@types/ini": "^4.1.1",
"@types/js-yaml": "^4.0.9",
"@types/minimist": "^1.2.5",
"@types/node": "^22.9.1",
"@types/node": "^22.9.3",
"@types/node-cron": "^3.0.11",
"@types/sinonjs__fake-timers": "^8.1.5",
"@typescript-eslint/eslint-plugin": "8.15.0",
Expand All @@ -92,8 +92,8 @@
"prettier": "^3.3.3",
"tslib": "^2.8.1",
"tsx": "^4.19.2",
"type-fest": "^4.27.0",
"typescript": "^5.7.0-beta",
"type-fest": "^4.28.0",
"typescript": "^5.7.2",
"uuid": "^11.0.3",
"vitest": "^2.1.5"
},
Expand Down
25 changes: 18 additions & 7 deletions src/helpers/logger.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { Get } from "type-fest";

import { TContext } from "./context.mts";
import { TBlackHole } from "./utilities.mts";

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface ReplacementLogger {
// intentionally left empty
// for use with declaration merging
}

export type GetLogger =
Get<ReplacementLogger, "logger"> extends object ? Get<ReplacementLogger, "logger"> : ILogger;

export type LogStreamTarget = (message: string, data: object) => TBlackHole;

export type DigitalAlchemyLogger = {
addTarget: (logger: GetLogger | LogStreamTarget) => void;
/**
* Create a new logger instance for a given context
*/
Expand All @@ -9,7 +24,7 @@ export type DigitalAlchemyLogger = {
* Retrieve a reference to the base logger used to emit from
*/
getBaseLogger: () => Record<
keyof ILogger,
keyof GetLogger,
(context: TContext, ...data: Parameters<TLoggerFunction>) => void
>;
getPrettyFormat: () => boolean;
Expand All @@ -22,23 +37,19 @@ export type DigitalAlchemyLogger = {
*
* Note: Extension still handles LOG_LEVEL logic
*/
setBaseLogger: (base: ILogger) => ILogger;
setBaseLogger: (base: GetLogger) => GetLogger;
/**
* Set the enabled/disabled state of the message pretty formatting logic
*/
setPrettyFormat: (state: boolean) => boolean;
/**
* Logger instance of last resort
*/
systemLogger: ILogger;
systemLogger: GetLogger;
/**
* exposed for testing
*/
updateShouldLog: () => void;
/**
* If set, logs will be converted to json & sent to target
*/
setHttpLogs: (url: string) => void;
};

export type TLoggerFunction =
Expand Down
26 changes: 20 additions & 6 deletions src/helpers/wiring.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TContext } from "./context.mts";
import { CronExpression, ScheduleRemove } from "./cron.mts";
import { BootstrapException } from "./errors.mts";
import { TLifecycleBase } from "./lifecycle.mts";
import { ILogger, TConfigLogLevel } from "./logger.mts";
import { GetLogger, TConfigLogLevel } from "./logger.mts";
import { TBlackHole } from "./utilities.mts";

export type TServiceReturn<OBJECT extends object = object> = void | OBJECT;
Expand Down Expand Up @@ -142,7 +142,14 @@ export type TInjectedConfig = {
// #region Special
// SEE DOCS http://docs.digital-alchemy.app/docs/core/declaration-merging
export interface AsyncLogData {
logger?: ILogger;
/**
* return ms since entry, precision is on you
*/
duration?: () => number;
/**
* thread local child logger
*/
logger?: GetLogger;
}

export interface AsyncLocalData {
Expand Down Expand Up @@ -188,7 +195,7 @@ export type TServiceParams = {
/**
* context aware logger instance
*/
logger: ILogger;
logger: GetLogger;
/**
* run commands on intervals & schedules
*
Expand Down Expand Up @@ -355,7 +362,7 @@ export type BootstrapOptions = {
/**
* use this logger, instead of the baked in one. Maybe you want some custom transports or something? Put your customized thing here
*/
customLogger?: ILogger;
customLogger?: GetLogger;

/**
* fine tine the built in logger
Expand Down Expand Up @@ -429,6 +436,13 @@ export type LoggerOptions = {
* Override the `LOG_LEVEL` per service or module
*/
levelOverrides?: Partial<Record<LoadedModuleNames | FlatServiceNames, TConfigLogLevel>>;

/**
* default: true (unless a replacement logger is provided)
*
* emit logs to stdout
*/
stdOut?: boolean;
};

export const WIRE_PROJECT = Symbol.for("wire-project");
Expand Down Expand Up @@ -467,7 +481,7 @@ export type ApplicationDefinition<
C extends OptionalModuleConfiguration,
> = ApplicationConfigurationOptions<S, C> &
Wire & {
logger: ILogger;
logger: GetLogger;
type: "application";
booted: boolean;
bootstrap: (options?: BootstrapOptions) => Promise<TServiceParams>;
Expand All @@ -478,7 +492,7 @@ export type TLibrary = LibraryDefinition<ServiceMap, OptionalModuleConfiguration
// #MARK: buildSortOrder
export function buildSortOrder<S extends ServiceMap, C extends OptionalModuleConfiguration>(
app: ApplicationDefinition<S, C>,
logger: ILogger,
logger: GetLogger,
) {
if (is.empty(app.libraries)) {
return [];
Expand Down
Loading

0 comments on commit e7f2a63

Please sign in to comment.