Skip to content

Commit

Permalink
Added getLogger to GasketActions (#977)
Browse files Browse the repository at this point in the history
* fix: types

* Update lock file

* Added missing deps.

* Update docusaurus preset

* Use main lock file

* Regen lock file

* Use @docusaurus/preset-classic 3.5.2

* update lock

* lock file

---------

Co-authored-by: Kawika Bader <[email protected]>
  • Loading branch information
kbader-godaddy and kawikabader authored Nov 21, 2024
1 parent dafbb69 commit 30dc358
Show file tree
Hide file tree
Showing 13 changed files with 1,383 additions and 987 deletions.
2,235 changes: 1,276 additions & 959 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"homepage": "https://github.com/godaddy/gasket#readme",
"devDependencies": {
"@docusaurus/core": "^3.5.2",
"@docusaurus/preset-classic": "^3.5.2",
"@docusaurus/preset-classic": "3.5.2",
"@docusaurus/theme-mermaid": "^3.5.2",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"eslint": "^8.56.0",
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@gasket/core": "^7.0.9",
"@testing-library/react": "^13.4.0",
"@types/jest": "^29.5.12",
"cross-env": "^7.0.3",
"cypress": "^13.6.6",
"eslint": "^8.56.0",
"eslint-config-godaddy": "^7.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/gasket-plugin-docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"devDependencies": {
"@docusaurus/core": "^3.4.0",
"@docusaurus/preset-classic": "^3.4.0",
"@docusaurus/preset-classic": "^3.5.2",
"@gasket/core": "^7.0.9",
"ajv": "^8.17.1",
"cross-env": "^7.0.3",
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-elastic-apm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
"devDependencies": {
"@gasket/core": "^7.0.9",
"cross-env": "^7.0.3",
"dotenv": "^16.4.5",
"elastic-apm-node": "^4.4.1",
"eslint": "^8.56.0",
Expand Down
19 changes: 17 additions & 2 deletions packages/gasket-plugin-logger/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import type { MaybeAsync, Plugin } from '@gasket/core';
import type { MaybeAsync, Plugin, Hook, HookId } from '@gasket/core';
import type { Logger } from '@gasket/plugin-logger';

export type LogLevel = 'error' | 'warn' | 'info' | 'debug';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type LogMethods = Record<LogLevel, (...args: any[]) => void>;

export function createChildLogger(
parent: {
[K in HookId]?: Hook<K>;
},
metadata: Record<string, any>
): Logger;

export function verifyLoggerLevels(logger: Logger): void;

declare module '@gasket/plugin-logger' {
export interface Logger extends LogMethods {
close?: () => MaybeAsync<any>;
Expand All @@ -19,7 +30,11 @@ declare module '@gasket/core' {
}

export interface HookExecTypes {
createLogger(): Logger
createLogger(): Logger;
}

export interface GasketActions {
getLogger(): Logger;
}
}

Expand Down
37 changes: 14 additions & 23 deletions packages/gasket-plugin-logger/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
/// <reference types="create-gasket-app" />
/// <reference types="@gasket/plugin-https" />
/// <reference types="@gasket/plugin-metadata" />

/* eslint-disable no-console, no-sync */
const { name, version, description } = require('../package.json');
const { createChildLogger, verifyLoggerLevels } = require('./utils');

function createChildLogger(parent, metadata) {
return {
...parent,
debug: (...args) => console.debug(...args, metadata),
error: (...args) => console.error(...args, metadata),
info: (...args) => console.info(...args, metadata),
warn: (...args) => console.warn(...args, metadata),
child: (meta) => createChildLogger(this, { ...metadata, ...meta })
};
}

function verifyLoggerLevels(logger) {
['debug', 'error', 'info', 'warn', 'child'].forEach((level) => {
if (typeof logger[level] !== 'function') {
throw new Error(`Logger is missing required level: ${level}`);
}
});
}

module.exports = {
/** @type {import('@gasket/core').Plugin} */
const plugin = {
name,
version,
description,
actions: {
getLogger: gasket => gasket.logger
getLogger: (gasket) => gasket.logger
},
hooks: {
create(gasket, { pkg, gasketConfig }) {
Expand All @@ -35,10 +22,12 @@ module.exports = {
});
},
init(gasket) {
// eslint-disable-next-line no-sync
const loggers = gasket.execSync('createLogger');

if (loggers && loggers.some((logger) => logger && logger instanceof Promise)) {
if (
loggers &&
loggers.some((logger) => logger && logger instanceof Promise)
) {
throw new Error('createLogger hooks must be synchronous');
}

Expand Down Expand Up @@ -85,3 +74,5 @@ module.exports = {
}
}
};

module.exports = plugin;
35 changes: 35 additions & 0 deletions packages/gasket-plugin-logger/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable no-console */
/**
* Create a child logger with additional metadata.
* @type {import('.').createChildLogger}
*/
function createChildLogger(parent, metadata) {
return {
...parent,
debug: (...args) => console.debug(...args, metadata),
error: (...args) => console.error(...args, metadata),
info: (...args) => console.info(...args, metadata),
warn: (...args) => console.warn(...args, metadata),
child: (meta) => createChildLogger(this, { ...metadata, ...meta })
};
}

/**
* Verify that the logger has all required levels.
* @type {import('.').verifyLoggerLevels}
*/
function verifyLoggerLevels(logger) {
/** @type {Array<keyof import('.').Logger>} */
const requiredLevels = ['debug', 'error', 'info', 'warn', 'child'];

requiredLevels.forEach((level) => {
if (typeof logger[level] !== 'function') {
throw new Error(`Logger is missing required level: ${level}`);
}
});
}

module.exports = {
createChildLogger,
verifyLoggerLevels
};
13 changes: 12 additions & 1 deletion packages/gasket-plugin-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint-config-godaddy": "^7.1.1",
"eslint-config-godaddy-typescript": "^4.0.3",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-unicorn": "^55.0.0",
"jest": "^29.7.0",
Expand All @@ -59,6 +60,16 @@
],
"rules": {
"unicorn/filename-case": "error"
}
},
"overrides": [
{
"files": [
"*.ts"
],
"extends": [
"godaddy-typescript"
]
}
]
}
}
21 changes: 21 additions & 0 deletions packages/gasket-plugin-logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"skipLibCheck": true,
"noImplicitAny": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": [
"esnext",
"dom"
],
"types": [
"@types/jest"
]
},
"include": [
"lib"
]
}
1 change: 1 addition & 0 deletions packages/gasket-plugin-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"devDependencies": {
"@types/compression": "^1.7.5",
"@types/cookie-parser": "^1.4.7",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint-config-godaddy": "^7.1.1",
"eslint-plugin-jest": "^28.6.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/gasket-plugin-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@
"@types/jest": "^29.5.12",
"@types/mocha": "^10.0.7",
"babel-eslint": "^10.1.0",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint-config-godaddy": "^7.1.1",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-react": "^7.35.0",
"eslint-plugin-unicorn": "^55.0.0",
"global-jsdom": "^8.4.0",
"jest": "^29.7.0",
"jsdom": "^20.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.12.5",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint-config-godaddy": "^7.1.1",
"eslint-plugin-jest": "^28.6.0",
Expand Down

0 comments on commit 30dc358

Please sign in to comment.