@govuk-one-login/frontend-logger 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/package.json +40 -0
- package/project.json +17 -0
- package/rollup.config.js +32 -0
- package/sonar-project.properties +16 -0
- package/src/__tests__/index.test.ts +93 -0
- package/src/index.ts +62 -0
- package/tsconfig.json +66 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
## 0.0.1 (2026-05-07)
|
|
2
|
+
|
|
3
|
+
### 🚀 Features
|
|
4
|
+
|
|
5
|
+
- **DFC-806:** Logger package ([6b0b5d3](https://github.com/govuk-one-login/govuk-one-login-frontend/commit/6b0b5d3))
|
|
6
|
+
|
|
7
|
+
### 🩹 Fixes
|
|
8
|
+
|
|
9
|
+
- **DFC-806:** Logger package version and name ([eddde3b](https://github.com/govuk-one-login/govuk-one-login-frontend/commit/eddde3b))
|
|
10
|
+
|
|
11
|
+
### ❤️ Thank You
|
|
12
|
+
|
|
13
|
+
- Sam Bunce
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@govuk-one-login/frontend-logger",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Shared logger for one-login-frontend packages",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "vitest --run --coverage",
|
|
13
|
+
"build": "rollup -c",
|
|
14
|
+
"dev": "rollup -c --watch"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/govuk-one-login/govuk-one-login-frontend.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"govuk-one-login",
|
|
22
|
+
"gds",
|
|
23
|
+
"govuk-onelogin",
|
|
24
|
+
"govuk one login"
|
|
25
|
+
],
|
|
26
|
+
"author": "DI Frontend Capability Team",
|
|
27
|
+
"license": "ISC",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/govuk-one-login/govuk-one-login-frontend/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/govuk-one-login/govuk-one-login-frontend#readme",
|
|
32
|
+
"devDependencies": {},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./build/esm/index.js",
|
|
36
|
+
"require": "./build/cjs/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"types": "./build/esm/index.d.ts"
|
|
40
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@govuk-one-login/frontend-logger",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"projectType": "library",
|
|
5
|
+
"sourceRoot": "packages/frontend-logger/src",
|
|
6
|
+
"// targets": "to see all targets run: nx show project frontend-logger --web",
|
|
7
|
+
"targets": {
|
|
8
|
+
"lint": {
|
|
9
|
+
"executor": "@nx/eslint:lint",
|
|
10
|
+
"options": {
|
|
11
|
+
"lintFilePatterns": [
|
|
12
|
+
"packages/frontend-logger/src/**/*.{js,ts,json}"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import json from "@rollup/plugin-json";
|
|
2
|
+
import typescript from "rollup-plugin-typescript2";
|
|
3
|
+
import copy from "rollup-plugin-copy";
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
external: ["pino"],
|
|
7
|
+
input: "src/index.ts",
|
|
8
|
+
output: [
|
|
9
|
+
{
|
|
10
|
+
file: "build/cjs/index.cjs",
|
|
11
|
+
format: "cjs",
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
file: "build/esm/index.js",
|
|
15
|
+
format: "es",
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
plugins: [
|
|
19
|
+
typescript(),
|
|
20
|
+
json(),
|
|
21
|
+
copy({
|
|
22
|
+
targets: [
|
|
23
|
+
{
|
|
24
|
+
src: "./build/cjs/index.d.ts",
|
|
25
|
+
dest: "./build/cjs/",
|
|
26
|
+
rename: "index.d.cts",
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
hook: "closeBundle",
|
|
30
|
+
}),
|
|
31
|
+
],
|
|
32
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
sonar.projectKey=govuk-one-login_frontend-logger
|
|
2
|
+
sonar.organization=govuk-one-login
|
|
3
|
+
|
|
4
|
+
# This is the name and version displayed in the SonarCloud UI.
|
|
5
|
+
sonar.projectName=frontend-logger
|
|
6
|
+
sonar.projectVersion=1.0
|
|
7
|
+
|
|
8
|
+
# Language-specific settings
|
|
9
|
+
sonar.language=typescript
|
|
10
|
+
|
|
11
|
+
sonar.sources=src/
|
|
12
|
+
|
|
13
|
+
sonar.tests=src/
|
|
14
|
+
sonar.test.inclusions=**/*.test.ts,**/*.spec.ts
|
|
15
|
+
|
|
16
|
+
sonar.sourceEncoding=UTF-8
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createLogger,
|
|
3
|
+
getLogger,
|
|
4
|
+
resetLogger,
|
|
5
|
+
setCustomLogger,
|
|
6
|
+
type Logger,
|
|
7
|
+
} from "..";
|
|
8
|
+
import pino from "pino";
|
|
9
|
+
|
|
10
|
+
// Custom matcher for pseudo-deep equality: JSON string (does not include all properties), log level, name
|
|
11
|
+
expect.extend({
|
|
12
|
+
toMatchLogger(received, expected) {
|
|
13
|
+
const { isNot } = this;
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
pass:
|
|
17
|
+
JSON.stringify(expected) === JSON.stringify(received) &&
|
|
18
|
+
expected?.level &&
|
|
19
|
+
expected.level === received.level &&
|
|
20
|
+
JSON.stringify(expected.bindings && expected.bindings()) ===
|
|
21
|
+
JSON.stringify(received.bindings && received.bindings()),
|
|
22
|
+
message: () =>
|
|
23
|
+
`${received} ${isNot ? "matches" : "does not match"} ${expected}.
|
|
24
|
+
Expected: ${JSON.stringify({ logLevel: expected.level, bindings: expected.bindings && expected.bindings() })}
|
|
25
|
+
Received: ${JSON.stringify({ logLevel: received.level, bindings: received.bindings && received.bindings() })}
|
|
26
|
+
`,
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const defaultLogger = pino({
|
|
32
|
+
name: "@govuk-one-login/default-logger",
|
|
33
|
+
level: "warn",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const customLogger = {
|
|
37
|
+
trace: vi.fn(),
|
|
38
|
+
warn: vi.fn(),
|
|
39
|
+
info: vi.fn(),
|
|
40
|
+
error: vi.fn(),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
describe("logger", () => {
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
resetLogger();
|
|
46
|
+
vi.resetAllMocks();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should create a default logger on first get if none exists", () => {
|
|
50
|
+
expect(getLogger() as Logger).toMatchLogger(defaultLogger);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should use a custom logger when one is provided", () => {
|
|
54
|
+
setCustomLogger(customLogger);
|
|
55
|
+
expect(customLogger).not.toMatchLogger(defaultLogger);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should warn if a logger is being overwritten by a custom logger", () => {
|
|
59
|
+
const warnSpy = vi.spyOn(getLogger(), "warn");
|
|
60
|
+
setCustomLogger(customLogger);
|
|
61
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
62
|
+
"Setting custom logger when one already exists",
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should return a customised pino logger if one has been created", () => {
|
|
67
|
+
const customisedLogger = createLogger({
|
|
68
|
+
level: "error",
|
|
69
|
+
name: "test",
|
|
70
|
+
});
|
|
71
|
+
expect(customisedLogger).not.toMatchLogger(defaultLogger);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should warn if a logger is being overwritten by a customised pino logger", () => {
|
|
75
|
+
const warnSpy = vi.spyOn(getLogger(), "warn");
|
|
76
|
+
createLogger({ level: "info" });
|
|
77
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
78
|
+
"Creating new logger with config when one already exists",
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should return to using the default logger after being reset", () => {
|
|
83
|
+
createLogger({ name: "@govuk-one-login/default-logger", level: "error" });
|
|
84
|
+
expect(getLogger()).toMatchLogger(
|
|
85
|
+
pino({
|
|
86
|
+
name: "@govuk-one-login/default-logger",
|
|
87
|
+
level: "error",
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
resetLogger();
|
|
91
|
+
expect(getLogger()).toMatchLogger(defaultLogger);
|
|
92
|
+
});
|
|
93
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Logger as PinoLogger, LoggerOptions } from "pino";
|
|
2
|
+
import { pino } from "pino";
|
|
3
|
+
|
|
4
|
+
export type TLogLevel =
|
|
5
|
+
| "fatal"
|
|
6
|
+
| "error"
|
|
7
|
+
| "warn"
|
|
8
|
+
| "info"
|
|
9
|
+
| "debug"
|
|
10
|
+
| "trace"
|
|
11
|
+
| "silent";
|
|
12
|
+
|
|
13
|
+
export type CustomLogger = {
|
|
14
|
+
trace: (message: unknown) => void;
|
|
15
|
+
warn: (message: unknown) => void;
|
|
16
|
+
info: (message: unknown) => void;
|
|
17
|
+
error: (message: unknown) => void;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type Logger = PinoLogger | CustomLogger;
|
|
21
|
+
|
|
22
|
+
let logger: Logger | undefined;
|
|
23
|
+
|
|
24
|
+
const initialiseDefaultLogger = () => {
|
|
25
|
+
logger = pino({
|
|
26
|
+
name: "@govuk-one-login/default-logger",
|
|
27
|
+
level: process.env.LOG_LEVEL ?? process.env.LOGS_LEVEL ?? "warn",
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const setCustomLogger = (customLogger: CustomLogger) => {
|
|
32
|
+
if (logger) {
|
|
33
|
+
logger.warn("Setting custom logger when one already exists");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
logger = customLogger;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const getLogger = (): Logger => {
|
|
40
|
+
if (!logger) {
|
|
41
|
+
initialiseDefaultLogger();
|
|
42
|
+
if (!logger) {
|
|
43
|
+
throw new Error("Error initialising default logger");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return logger;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const createLogger = (loggerOptions: LoggerOptions) => {
|
|
51
|
+
if (logger) {
|
|
52
|
+
logger.warn("Creating new logger with config when one already exists");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
logger = pino(loggerOptions);
|
|
56
|
+
|
|
57
|
+
return logger;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const resetLogger = () => {
|
|
61
|
+
logger = undefined;
|
|
62
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"include": ["./src"],
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
/* Basic Options */
|
|
5
|
+
// "incremental": true, /* Enable incremental compilation */
|
|
6
|
+
"target": "ES2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
|
|
7
|
+
"module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
|
|
8
|
+
"lib": [
|
|
9
|
+
"es2019"
|
|
10
|
+
] /* Specify library files to be included in the compilation. */,
|
|
11
|
+
// "allowJs": true, /* Allow javascript files to be compiled. */
|
|
12
|
+
// "checkJs": true, /* Report errors in .js files. */
|
|
13
|
+
// "jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
|
14
|
+
"declaration": true /* Generates corresponding '.d.ts' file. */,
|
|
15
|
+
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
|
|
16
|
+
"sourceMap": true /* Generates corresponding '.map' file. */,
|
|
17
|
+
// "outFile": "./", /* Concatenate and emit output to single file. */
|
|
18
|
+
"outDir": "./dist" /* Redirect output structure to the directory. */,
|
|
19
|
+
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
|
|
20
|
+
// "composite": true, /* Enable project compilation */
|
|
21
|
+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
|
22
|
+
// "removeComments": true, /* Do not emit comments to output. */
|
|
23
|
+
// "noEmit": true, /* Do not emit outputs. */
|
|
24
|
+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
|
25
|
+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
|
26
|
+
"isolatedModules": true /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */,
|
|
27
|
+
/* Strict Type-Checking Options */
|
|
28
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
29
|
+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
|
30
|
+
// "strictNullChecks": true, /* Enable strict null checks. */
|
|
31
|
+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
|
32
|
+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
|
33
|
+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
|
34
|
+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
|
35
|
+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
|
36
|
+
/* Additional Checks */
|
|
37
|
+
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
|
38
|
+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
|
39
|
+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
|
40
|
+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
|
41
|
+
/* Module Resolution Options */
|
|
42
|
+
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
|
|
43
|
+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
|
44
|
+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
|
45
|
+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
|
46
|
+
// "typeRoots": [], /* List of folders to include type definitions from. */
|
|
47
|
+
"types": [
|
|
48
|
+
"vitest/globals"
|
|
49
|
+
] /* Type declaration files to be included in compilation. */,
|
|
50
|
+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
|
51
|
+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
|
|
52
|
+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
|
53
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
54
|
+
/* Source Map Options */
|
|
55
|
+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
|
56
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
57
|
+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
|
58
|
+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
|
59
|
+
/* Experimental Options */
|
|
60
|
+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
|
61
|
+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
|
62
|
+
/* Advanced Options */
|
|
63
|
+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
|
|
64
|
+
"resolveJsonModule": true
|
|
65
|
+
}
|
|
66
|
+
}
|