This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+9
View File
@@ -0,0 +1,9 @@
# The MIT License
Copyright 2022 Harminder Virk, contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+53
View File
@@ -0,0 +1,53 @@
<div align="center">
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px">
</div>
<br />
<div align="center">
<h3>AdonisJS Logger</h3>
<p>AdonisJS logger built on top of <a href="http://getpino.io/#/">pino</a> for speed and minimal runtime overhead.</p>
</div>
<br />
<div align="center">
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![synk-image]][synk-url]
</div>
<div align="center">
<h3>
<a href="https://adonisjs.com">
Website
</a>
<span> | </span>
<a href="https://docs.adonisjs.com/guides/logger">
Guides
</a>
<span> | </span>
<a href="CONTRIBUTING.md">
Contributing
</a>
</h3>
</div>
<div align="center">
<sub>Built with ❤︎ by <a href="https://twitter.com/AmanVirk1">Harminder Virk</a>
</div>
[gh-workflow-image]: https://img.shields.io/github/workflow/status/adonisjs/logger/test?style=for-the-badge
[gh-workflow-url]: https://github.com/adonisjs/logger/actions/workflows/test.yml "Github action"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
[npm-image]: https://img.shields.io/npm/v/@adonisjs/logger.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@adonisjs/logger "npm"
[license-image]: https://img.shields.io/npm/l/@adonisjs/logger?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"
[synk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/env?label=Synk%20Vulnerabilities&style=for-the-badge
[synk-url]: https://snyk.io/test/github/adonisjs/env?targetFile=package.json "synk"
+80
View File
@@ -0,0 +1,80 @@
/// <reference types="pino" />
declare module '@ioc:Adonis/Core/Logger' {
import { Level, TimeFn, redactOptions, PrettyOptions, SerializerFn, LevelMapping, Bindings, DestinationStream } from 'pino';
/**
* The formatters accepted by pino
*/
export type Formatters = {
level?(labelName: string, labelNumber: number): Object;
bindings?(bindings: Bindings): Object;
log?(log: Object): Object;
};
export type TimestampKeywords = 'iso' | 'unix' | 'epoch';
/**
* Config shape
*/
export type LoggerConfig = {
name: string;
level: Level | 'silent' | string;
enabled: boolean;
messageKey?: string;
safe?: boolean;
crlf?: boolean;
timestamp?: TimeFn | TimestampKeywords | boolean;
customLevels?: {
[key: string]: number;
};
formatters?: Formatters;
useOnlyCustomLevels?: boolean;
redact?: string[] | redactOptions;
prettyPrint?: boolean | PrettyOptions;
base?: {
[key: string]: any;
} | null;
serializers?: {
[key: string]: SerializerFn;
};
stream?: DestinationStream;
} & {
[key: string]: any;
};
/**
* Logger interface that main and fake logger implements
*/
export interface LoggerContract {
level: string;
levelNumber: number;
levels: LevelMapping;
pinoVersion: string;
log(level: string, message: string, ...values: any[]): void;
log(level: string, mergingObject: any, message: string, ...values: any[]): void;
trace(message: string, ...values: any[]): void;
trace(mergingObject: any, message: string, ...values: any[]): void;
debug(message: string, ...values: any[]): void;
debug(mergingObject: any, message: string, ...values: any[]): void;
info(message: string, ...values: any[]): void;
info(mergingObject: any, message: string, ...values: any[]): void;
warn(message: string, ...values: any[]): void;
warn(mergingObject: any, message: string, ...values: any[]): void;
error(message: string, ...values: any[]): void;
error(mergingObject: any, message: string, ...values: any[]): void;
fatal(message: string, ...values: any[]): void;
fatal(mergingObject: any, message: string, ...values: any[]): void;
isLevelEnabled(level: string): boolean;
bindings(): Bindings;
child(bindings: {
serializers?: {
[key: string]: SerializerFn;
};
[key: string]: any;
}, options?: {
level?: Level | string;
redact?: string[] | redactOptions;
serializers?: {
[key: string]: SerializerFn;
};
}): LoggerContract;
}
const Logger: LoggerContract;
export default Logger;
}
+8
View File
@@ -0,0 +1,8 @@
/*
* @adonisjs/logger
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+2
View File
@@ -0,0 +1,2 @@
export { Logger } from './src/Logger';
export { FakeLogger } from './src/FakeLogger';
+15
View File
@@ -0,0 +1,15 @@
"use strict";
/*
* @adonisjs/logger
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FakeLogger = exports.Logger = void 0;
var Logger_1 = require("./src/Logger");
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return Logger_1.Logger; } });
var FakeLogger_1 = require("./src/FakeLogger");
Object.defineProperty(exports, "FakeLogger", { enumerable: true, get: function () { return FakeLogger_1.FakeLogger; } });
+30
View File
@@ -0,0 +1,30 @@
/// <reference path="../adonis-typings/logger.d.ts" />
import Pino from 'pino';
import { LoggerConfig } from '@ioc:Adonis/Core/Logger';
import { Logger } from './Logger';
/**
* Fake logger that sets a custom logger stream and returns
* the log messages as an array vs writing them to `stdout`.
*/
export declare class FakeLogger extends Logger {
constructor(config: LoggerConfig, pino?: Pino.Logger);
/**
* An array of in-memory logs
*/
get logs(): any[];
/**
* Returns the child fake logger. All logs from the child
* are writte to the same top level stream
*/
child(bindings: {
level?: Pino.Level | string;
serializers?: {
[key: string]: Pino.SerializerFn;
};
[key: string]: any;
}): FakeLogger;
/**
* Clear in-memory logs
*/
clear(): void;
}
+65
View File
@@ -0,0 +1,65 @@
"use strict";
/*
* @adonisjs/logger
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FakeLogger = void 0;
const Logger_1 = require("./Logger");
/**
* Fake logger that sets a custom logger stream and returns
* the log messages as an array vs writing them to `stdout`.
*/
class FakeLogger extends Logger_1.Logger {
constructor(config, pino) {
/**
* Config is only used when we are not receiving an
* existing instance of pino.
*/
if (!pino) {
const cloned = Object.assign({}, config, {
prettyPrint: false,
stream: {
logs: [],
write: function writer(line) {
const log = JSON.parse(line);
delete log.timestamp;
this.logs.push(log);
},
},
});
super(cloned);
}
else {
super(config, pino);
}
}
/**
* An array of in-memory logs
*/
get logs() {
return this.config.stream.logs;
}
/**
* Returns the child fake logger. All logs from the child
* are writte to the same top level stream
*/
child(bindings) {
if (!this.config.enabled) {
return this;
}
return new FakeLogger(this.config, this.pino.child(bindings));
}
/**
* Clear in-memory logs
*/
clear() {
;
this.config.stream.logs = [];
}
}
exports.FakeLogger = FakeLogger;
+92
View File
@@ -0,0 +1,92 @@
/// <reference path="../adonis-typings/logger.d.ts" />
import Pino from 'pino';
import { LoggerConfig, LoggerContract } from '@ioc:Adonis/Core/Logger';
/**
* Logger class built on top of pino with couple of changes in
* the configuration. You can access the underlying `pino`
* object using `logger.pino`.
*/
export declare class Logger implements LoggerContract {
protected config: LoggerConfig;
pino: Pino.Logger;
constructor(config: LoggerConfig, pino?: Pino.Logger);
/**
* A map of levels
*/
get levels(): Pino.LevelMapping;
/**
* Returns the current logger level
*/
get level(): string;
/**
* Update logger level
*/
set level(level: string);
/**
* Returns the current logger level number
*/
get levelNumber(): number;
/**
* Returns the pino version
*/
get pinoVersion(): string;
/**
* Returns a boolean telling if level is enabled or
* not.
*/
isLevelEnabled(level: string): boolean;
/**
* Log message for any named level
*/
log(level: string, message: string, ...values: any[]): void;
log(level: string, mergingObject: any, message: string, ...values: any[]): void;
/**
* Log message at trace level
*/
trace(message: string, ...values: any[]): void;
trace(mergingObject: any, message: string, ...values: any[]): void;
/**
* Log message at debug level
*/
debug(message: string, ...values: any[]): void;
debug(mergingObject: any, message: string, ...values: any[]): void;
/**
* Log message at info level
*/
info(message: string, ...values: any[]): void;
info(mergingObject: any, message: string, ...values: any[]): void;
/**
* Log message at warn level
*/
warn(message: string, ...values: any[]): void;
warn(mergingObject: any, message: string, ...values: any[]): void;
/**
* Log message at error level
*/
error(message: string, ...values: any[]): void;
error(mergingObject: any, message: string, ...values: any[]): void;
/**
* Log message at fatal level
*/
fatal(message: string, ...values: any[]): void;
fatal(mergingObject: any, message: string, ...values: any[]): void;
/**
* Returns a child logger instance
*/
child(bindings: {
serializers?: {
[key: string]: Pino.SerializerFn;
};
[key: string]: any;
}, options?: {
level?: Pino.Level | string;
redact?: string[] | Pino.redactOptions;
serializers?: {
[key: string]: Pino.SerializerFn;
};
}): Logger;
/**
* Returns default bindings for the logger
*/
bindings(): Pino.Bindings;
}
+153
View File
@@ -0,0 +1,153 @@
"use strict";
/*
* @adonisjs/logger
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
/// <reference path="../adonis-typings/logger.ts" />
const pino_1 = __importDefault(require("pino"));
const abstract_logging_1 = __importDefault(require("abstract-logging"));
const getPino_1 = require("./getPino");
const STATIC_LEVELS = {
labels: {
10: 'trace',
20: 'debug',
30: 'info',
40: 'warn',
50: 'error',
60: 'fatal',
},
values: {
fatal: 60,
error: 50,
warn: 40,
info: 30,
debug: 20,
trace: 10,
},
};
/**
* Logger class built on top of pino with couple of changes in
* the configuration. You can access the underlying `pino`
* object using `logger.pino`.
*/
class Logger {
constructor(config, pino) {
this.config = config;
if (!this.config.enabled) {
this.pino = abstract_logging_1.default;
}
else {
this.pino = pino || (0, getPino_1.getPino)(this.config);
}
}
/**
* A map of levels
*/
get levels() {
if (!this.config.enabled) {
return STATIC_LEVELS;
}
return this.pino.levels;
}
/**
* Returns the current logger level
*/
get level() {
if (!this.config.enabled) {
return this.config.level;
}
return this.pino.level;
}
/**
* Update logger level
*/
set level(level) {
if (!this.config.enabled) {
this.config.level = level;
return;
}
this.pino.level = level;
}
/**
* Returns the current logger level number
*/
get levelNumber() {
if (!this.config.enabled) {
return STATIC_LEVELS.values[this.config.level];
}
return this.pino.levelVal;
}
/**
* Returns the pino version
*/
get pinoVersion() {
return pino_1.default.version;
}
/**
* Returns a boolean telling if level is enabled or
* not.
*/
isLevelEnabled(level) {
if (!this.config.enabled) {
return false;
}
return this.pino.isLevelEnabled(level);
}
log(level, mergingObject, message, ...values) {
if (values.length) {
this.pino[level](mergingObject, message, ...values);
}
else if (message) {
this.pino[level](mergingObject, message);
}
else {
this.pino[level](mergingObject);
}
}
trace(mergingObject, message, ...values) {
this.log('trace', mergingObject, message, ...values);
}
debug(mergingObject, message, ...values) {
this.log('debug', mergingObject, message, ...values);
}
info(mergingObject, message, ...values) {
this.log('info', mergingObject, message, ...values);
}
warn(mergingObject, message, ...values) {
this.log('warn', mergingObject, message, ...values);
}
error(mergingObject, message, ...values) {
this.log('error', mergingObject, message, ...values);
}
fatal(mergingObject, message, ...values) {
this.log('fatal', mergingObject, message, ...values);
}
/**
* Returns a child logger instance
*/
child(bindings, options) {
if (!this.config.enabled) {
return this;
}
return new Logger(this.config, this.pino.child(bindings, options));
}
/**
* Returns default bindings for the logger
*/
bindings() {
if (!this.config.enabled) {
return {};
}
return this.pino.bindings();
}
}
exports.Logger = Logger;
+7
View File
@@ -0,0 +1,7 @@
/// <reference path="../adonis-typings/logger.d.ts" />
import Pino from 'pino';
import { LoggerConfig } from '@ioc:Adonis/Core/Logger';
/**
* Returns an instance of pino logger by adjusting the config options
*/
export declare function getPino(options: LoggerConfig): Pino.Logger;
+40
View File
@@ -0,0 +1,40 @@
"use strict";
/*
* @adonisjs/logger
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPino = void 0;
/// <reference path="../adonis-typings/logger.ts" />
const pino_1 = __importDefault(require("pino"));
/**
* Mapping pino timestamp formatters to keywords
*/
const TimestampFormatters = {
iso: pino_1.default.stdTimeFunctions.isoTime,
epoch: pino_1.default.stdTimeFunctions.epochTime,
unix: pino_1.default.stdTimeFunctions.unixTime,
};
/**
* Returns an instance of pino logger by adjusting the config options
*/
function getPino(options) {
const pinoOptions = Object.assign({}, options);
/**
* Use pino formatters when a keyword is used
*/
if (pinoOptions.timestamp &&
typeof pinoOptions.timestamp === 'string' &&
TimestampFormatters[pinoOptions.timestamp]) {
pinoOptions.timestamp = TimestampFormatters[pinoOptions.timestamp];
}
return options.stream ? (0, pino_1.default)(pinoOptions, options.stream) : (0, pino_1.default)(pinoOptions);
}
exports.getPino = getPino;
+149
View File
@@ -0,0 +1,149 @@
{
"name": "@adonisjs/logger",
"version": "4.1.5",
"description": "Logger built on top of pino to be used by AdonisJs",
"main": "build/index.js",
"files": [
"build/adonis-typings",
"build/providers",
"build/src",
"build/index.d.ts",
"build/index.js"
],
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"pretest": "npm run lint",
"test": "node -r @adonisjs/require-ts/build/register bin/test.ts",
"clean": "del build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile",
"commit": "git-cz",
"release": "np --message=\"chore(release): %s\"",
"version": "npm run build",
"format": "prettier --write .",
"prepublishOnly": "npm run build",
"lint": "eslint . --ext=.ts",
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json adonisjs/logger"
},
"keywords": [
"logger",
"logging",
"pino",
"adonisjs"
],
"author": "virk,adonisjs",
"license": "MIT",
"devDependencies": {
"@adonisjs/mrm-preset": "^5.0.3",
"@adonisjs/require-ts": "^2.0.13",
"@japa/assert": "^1.3.6",
"@japa/run-failed-tests": "^1.1.0",
"@japa/runner": "^2.2.2",
"@japa/spec-reporter": "^1.3.2",
"@types/node": "^18.11.3",
"commitizen": "^4.2.5",
"cz-conventional-changelog": "^3.3.0",
"del-cli": "^5.0.0",
"eslint": "^8.25.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-adonis": "^2.1.1",
"eslint-plugin-prettier": "^4.2.1",
"github-label-sync": "^2.2.0",
"husky": "^8.0.1",
"mrm": "^4.1.13",
"np": "^7.6.2",
"pino-pretty": "^7.6.1",
"prettier": "^2.7.1",
"typescript": "^4.8.4"
},
"nyc": {
"exclude": [
"test"
],
"extension": [
".ts"
]
},
"husky": {
"hooks": {
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js"
}
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"np": {
"contents": ".",
"anyBranch": false
},
"dependencies": {
"@poppinss/utils": "^5.0.0",
"@types/pino": "^6.3.12",
"abstract-logging": "^2.0.1",
"pino": "^6.14.0"
},
"directories": {
"doc": "docs",
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/adonisjs/logger.git"
},
"bugs": {
"url": "https://github.com/adonisjs/logger/issues"
},
"homepage": "https://github.com/adonisjs/logger#readme",
"adonisjs": {
"exceptions": "./build/exceptions.json"
},
"publishConfig": {
"access": "public",
"tag": "latest"
},
"mrmConfig": {
"core": true,
"license": "MIT",
"services": [
"github-actions"
],
"minNodeVersion": "14.15.4",
"probotApps": [
"stale",
"lock"
],
"runGhActionsOnWindows": false
},
"eslintConfig": {
"extends": [
"plugin:adonis/typescriptPackage",
"prettier"
],
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
}
},
"eslintIgnore": [
"build"
],
"prettier": {
"trailingComma": "es5",
"semi": false,
"singleQuote": true,
"useTabs": false,
"quoteProps": "consistent",
"bracketSpacing": true,
"arrowParens": "always",
"printWidth": 100
}
}