Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

logger4node

Package Overview
Dependencies
Maintainers
0
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logger4node - npm Package Compare versions

Comparing version 1.0.29 to 1.0.32

spec/test-json-annonomous.spec.d.ts

17

package.json
{
"name": "logger4node",
"version": "1.0.29",
"version": "1.0.32",
"description": "",

@@ -9,5 +9,4 @@ "main": "index.js",

"build": "rm -rf dist/* && tsc -p ./ && npm run copy-build-files",
"copy-build-files": "cp ./src/pino/pino.transform.js ./dist/src/pino/pino.transform.js && cp ./README.md ./dist/README.md && cp package.json ./dist/package.json && cp package-lock.json ./dist/package-lock.json",
"copy-test-logger": "cp ./spec/pino.logger.test.ts ./src/pino/pino.logger.ts",
"coverage": "npm run copy-test-logger && nyc mocha './spec/**/*.spec.ts' && nyc report --reporter=text-lcov > coverage1.lcov"
"copy-build-files": "cp ./README.md ./dist/README.md && cp package.json ./dist/package.json && cp package-lock.json ./dist/package-lock.json",
"coverage": "nyc mocha './spec/**/*.spec.ts' && nyc report --reporter=text-lcov > coverage1.lcov"
},

@@ -27,4 +26,2 @@ "repository": {

"async_hooks": "1.0.0",
"pino": "9.1.0",
"pino-abstract-transport": "1.2.0",
"util": "0.12.5",

@@ -37,6 +34,6 @@ "uuid": "9.0.1"

"@types/mocha": "10.0.6",
"@types/node": "20.12.12",
"@types/node": "20.14.8",
"@types/sinon": "17.0.3",
"@typescript-eslint/eslint-plugin": "7.9.0",
"@typescript-eslint/parser": "7.9.0",
"@typescript-eslint/parser": "7.14.1",
"chai": "4.4.1",

@@ -47,3 +44,3 @@ "eslint": "8.57.0",

"eslint-plugin-import": "2.29.1",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-promise": "6.2.0",
"eslint-plugin-typescript": "0.14.0",

@@ -54,3 +51,3 @@ "flush-write-stream": "^2.0.0",

"nyc": "15.1.0",
"sinon": "17.0.1",
"sinon": "18.0.0",
"sinon-chai": "^3.7.0",

@@ -57,0 +54,0 @@ "split2": "^4.2.0",

![build](https://github.com/yog27ray/logger4node/actions/workflows/node.js.yml/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/yog27ray/logger4node/branch/master/graph/badge.svg)](https://codecov.io/gh/yog27ray/logger4node)
##Logger for Node
## Logger for Node
`logger4node` is a simple and flexible logging library for Node.js applications.
It allows you to log messages with different levels of severity, and you can set different severity levels for different project files
## Setup:
1. Create Application Logger.
```ts
import { Logger4Node } from 'logger4node';
const applicationLogger = new Logger4Node('Application');
```
2. Set log pattern and default log level.
```ts
import { LogSeverity } from 'logger4node';
applicationLogger.setLogLevel(LogSeverity.ERROR);
applicationLogger.setLogPattern('Application:*');
```
3. Create file level logger.
```ts
const fileLogger = applicationLogger.instance('File1');
```
4. Log information
```ts
fileLogger.error('This is test log');
```
Output:
```text
Error: Application:File1 This is test log
```
## Configuration:
1. **JSON Logs**: Logging in json provide more information and is recommended for production deployments.
Json logging provide `time`, `source`, `request`, `extraData`, `stack`.
```ts
applicationLogger.setJsonLogging(true);
.
.
.
fileLogger.error('This is test log');
```
Output:
```text
{"level":"error","time":"2024-07-01T07:07:54.877Z","className":"Application:File1","source":{"caller":"callerFunctionName","fileName":"fileName.ts","path":"file path","line":"13","column":"10"},"message":"This is test log","request":{},"extra":{},"stack":""}
```
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -30,5 +7,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

const chai_1 = require("chai");
const fs = __importStar(require("node:fs"));
const sinon_1 = __importDefault(require("sinon"));
const tail_1 = require("tail");
const logger_1 = require("../src/logger/logger");

@@ -38,23 +13,2 @@ const logger4_node_1 = require("../src/logger/logger4-node");

const currentFolder = __dirname;
const spyConsoleLog = [];
const loggerSpy = {
log(_data) {
let data = _data;
const json = JSON.parse(data);
json.time = spyConsoleLog.length;
json.pid = 1;
json.hostname = 'hostname';
data = JSON.stringify(json);
console.log(JSON.stringify(json));
spyConsoleLog.push(data);
},
reset() {
spyConsoleLog.splice(0, spyConsoleLog.length);
},
};
fs.writeFileSync('./spec/test.logs', '', 'utf-8');
new tail_1.Tail('./spec/test.logs')
.on('line', (data) => loggerSpy.log(data))
.on('error', (error) => console.log(error))
.watch();
describe('Logger4nodeJSON', () => {

@@ -78,5 +32,8 @@ context('logging in different level', () => {

beforeEach(() => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*');
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger4_node_1.Logger4Node.setLogSeverityPattern(logSeverity, undefined));
logger1.setLogPattern('Logger1:*');
logger2.setLogPattern('Logger1:*');
logger1.setLogLevel(logger_1.LogSeverity.VERBOSE);
logger2.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger1.setLogSeverityPattern(logSeverity, undefined));
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger2.setLogSeverityPattern(logSeverity, undefined));
callbackSpy = sinon_1.default.spy(console, 'log');

@@ -86,23 +43,80 @@ });

await (0, test_logs_1.printLogsInDifferentLevel)(logger1Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(5);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"verbose","time":0,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"10","column":"10"},"message":"verbose log"}`);
(0, chai_1.expect)(callbackSpy.getCall(1).args.join(' ')).to
.equal('{"level":"debug","time":1,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"11","column":"10"},"message":"debug log"}`);
(0, chai_1.expect)(callbackSpy.getCall(2).args.join(' ')).to
.equal('{"level":"info","time":2,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"12","column":"10"},"message":"info log"}`);
(0, chai_1.expect)(callbackSpy.getCall(3).args.join(' ')).to
.equal('{"level":"warn","time":3,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"13","column":"10"},"message":"warn log"}`);
(0, chai_1.expect)(callbackSpy.getCall(4).args.join(' ')).to
.equal('{"level":"error","time":4,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"14","column":"10"},"message":"error log"}`);
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'verbose',
time: 0,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '13',
column: '10',
},
message: 'verbose log',
},
{
level: 'debug',
time: 1,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '14',
column: '10',
},
message: 'debug log',
},
{
level: 'info',
time: 2,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '15',
column: '10',
},
message: 'info log',
},
{
level: 'warn',
time: 3,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
{
level: 'error',
time: 4,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '17',
column: '10',
},
message: 'error log',
},
]);
});

@@ -114,58 +128,178 @@ it('should not print logger2 logs', async () => {

it('should allow print logger2 logs', async () => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*,Logger2:*');
logger2.setLogPattern('Logger1:*,Logger2:*');
logger2.setLogPattern('Logger1:*,Logger2:*');
await (0, test_logs_1.printLogsInDifferentLevel)(logger2Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(5);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"verbose","time":0,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"10","column":"10"},"message":"verbose log"}`);
(0, chai_1.expect)(callbackSpy.getCall(1).args.join(' ')).to
.equal('{"level":"debug","time":1,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"11","column":"10"},"message":"debug log"}`);
(0, chai_1.expect)(callbackSpy.getCall(2).args.join(' ')).to
.equal('{"level":"info","time":2,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"12","column":"10"},"message":"info log"}`);
(0, chai_1.expect)(callbackSpy.getCall(3).args.join(' ')).to
.equal('{"level":"warn","time":3,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"13","column":"10"},"message":"warn log"}`);
(0, chai_1.expect)(callbackSpy.getCall(4).args.join(' ')).to
.equal('{"level":"error","time":4,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"14","column":"10"},"message":"error log"}`);
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'verbose',
time: 0,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '13',
column: '10',
},
message: 'verbose log',
},
{
level: 'debug',
time: 1,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '14',
column: '10',
},
message: 'debug log',
},
{
level: 'info',
time: 2,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '15',
column: '10',
},
message: 'info log',
},
{
level: 'warn',
time: 3,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
{
level: 'error',
time: 4,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '17',
column: '10',
},
message: 'error log',
},
]);
});
it('should print only Logger1 Debug and above logs', async () => {
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.WARN);
logger1.setLogLevel(logger_1.LogSeverity.WARN);
logger2.setLogLevel(logger_1.LogSeverity.WARN);
await (0, test_logs_1.printLogsInDifferentLevel)(logger1Instance1);
await (0, test_logs_1.printLogsInDifferentLevel)(logger2Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(2);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"warn","time":0,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"13","column":"10"},"message":"warn log"}`);
(0, chai_1.expect)(callbackSpy.getCall(1).args.join(' ')).to
.equal('{"level":"error","time":1,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"14","column":"10"},"message":"error log"}`);
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'warn',
time: 0,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
{
level: 'error',
time: 1,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '17',
column: '10',
},
message: 'error log',
},
]);
});
it('should print only Logger1 Debug and above logs and logger2 only Debug: ', async () => {
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.WARN);
logger4_node_1.Logger4Node.setLogSeverityPattern(logger_1.LogSeverity.WARN, 'Logger2:*');
logger1.setLogLevel(logger_1.LogSeverity.WARN);
logger2.setLogLevel(logger_1.LogSeverity.WARN);
logger1.setLogSeverityPattern(logger_1.LogSeverity.WARN, 'Logger2:*');
logger2.setLogSeverityPattern(logger_1.LogSeverity.WARN, 'Logger2:*');
await (0, test_logs_1.printLogsInDifferentLevel)(logger1Instance1);
await (0, test_logs_1.printLogsInDifferentLevel)(logger2Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(3);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"warn","time":0,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"13","column":"10"},"message":"warn log"}`);
(0, chai_1.expect)(callbackSpy.getCall(1).args.join(' ')).to
.equal('{"level":"error","time":1,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"14","column":"10"},"message":"error log"}`);
(0, chai_1.expect)(callbackSpy.getCall(2).args.join(' ')).to
.equal('{"level":"warn","time":2,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"13","column":"10"},"message":"warn log"}`);
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'warn',
time: 0,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
{
level: 'error',
time: 1,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '17',
column: '10',
},
message: 'error log',
},
{
level: 'warn',
time: 2,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
]);
});

@@ -175,83 +309,243 @@ it('should print both instance of Logger1', async () => {

await (0, test_logs_1.printLogsInDifferentLevel)(logger1Instance2);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(10);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"verbose","time":0,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"10","column":"10"},"message":"verbose log"}`);
(0, chai_1.expect)(callbackSpy.getCall(1).args.join(' ')).to
.equal('{"level":"debug","time":1,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"11","column":"10"},"message":"debug log"}`);
(0, chai_1.expect)(callbackSpy.getCall(2).args.join(' ')).to
.equal('{"level":"info","time":2,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"12","column":"10"},"message":"info log"}`);
(0, chai_1.expect)(callbackSpy.getCall(3).args.join(' '))
.to.equal('{"level":"warn","time":3,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}","line":"13","column":"10"},"message":"warn log"}`);
(0, chai_1.expect)(callbackSpy.getCall(4).args.join(' ')).to
.equal('{"level":"error","time":4,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"14","column":"10"},"message":"error log"}');
(0, chai_1.expect)(callbackSpy.getCall(5).args.join(' ')).to
.equal('{"level":"verbose","time":5,"pid":1,"hostname":"hostname","className":"Logger1:Instance2","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"10","column":"10"},"message":"verbose log"}');
(0, chai_1.expect)(callbackSpy.getCall(6).args.join(' ')).to
.equal('{"level":"debug","time":6,"pid":1,"hostname":"hostname","className":"Logger1:Instance2","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"11","column":"10"},"message":"debug log"}');
(0, chai_1.expect)(callbackSpy.getCall(7).args.join(' ')).to
.equal('{"level":"info","time":7,"pid":1,"hostname":"hostname","className":"Logger1:Instance2","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"12","column":"10"},"message":"info log"}');
(0, chai_1.expect)(callbackSpy.getCall(8).args.join(' ')).to
.equal('{"level":"warn","time":8,"pid":1,"hostname":"hostname","className":"Logger1:Instance2","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"13","column":"10"},"message":"warn log"}');
(0, chai_1.expect)(callbackSpy.getCall(9).args.join(' ')).to
.equal('{"level":"error","time":9,"pid":1,"hostname":"hostname","className":"Logger1:Instance2","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"14","column":"10"},"message":"error log"}');
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'verbose',
time: 0,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '13',
column: '10',
},
message: 'verbose log',
},
{
level: 'debug',
time: 1,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '14',
column: '10',
},
message: 'debug log',
},
{
level: 'info',
time: 2,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '15',
column: '10',
},
message: 'info log',
},
{
level: 'warn',
time: 3,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
{
level: 'error',
time: 4,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '17',
column: '10',
},
message: 'error log',
},
{
level: 'verbose',
time: 5,
extra: {},
stack: '',
className: 'Logger1:Instance2',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '13',
column: '10',
},
message: 'verbose log',
},
{
level: 'debug',
time: 6,
extra: {},
stack: '',
className: 'Logger1:Instance2',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '14',
column: '10',
},
message: 'debug log',
},
{
level: 'info',
time: 7,
extra: {},
stack: '',
className: 'Logger1:Instance2',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '15',
column: '10',
},
message: 'info log',
},
{
level: 'warn',
time: 8,
extra: {},
stack: '',
className: 'Logger1:Instance2',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
{
level: 'error',
time: 9,
extra: {},
stack: '',
className: 'Logger1:Instance2',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '17',
column: '10',
},
message: 'error log',
},
]);
});
it('should print only instance1 of Logger1', async () => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*,-Logger1:Instance2*');
logger1.setLogPattern('Logger1:*,-Logger1:Instance2*');
logger2.setLogPattern('Logger1:*,-Logger1:Instance2*');
await (0, test_logs_1.printLogsInDifferentLevel)(logger1Instance1);
await (0, test_logs_1.printLogsInDifferentLevel)(logger1Instance2);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(5);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"verbose","time":0,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"10","column":"10"},"message":"verbose log"}');
(0, chai_1.expect)(callbackSpy.getCall(1).args.join(' ')).to
.equal('{"level":"debug","time":1,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source":'
+ '{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"11","column":"10"},"message":"debug log"}');
(0, chai_1.expect)(callbackSpy.getCall(2).args.join(' ')).to
.equal('{"level":"info","time":2,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source"'
+ ':{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"12","column":"10"},"message":"info log"}');
(0, chai_1.expect)(callbackSpy.getCall(3).args.join(' ')).to
.equal('{"level":"warn","time":3,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source"'
+ ':{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"13","column":"10"},"message":"warn log"}');
(0, chai_1.expect)(callbackSpy.getCall(4).args.join(' ')).to
.equal('{"level":"error","time":4,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source"'
+ ':{"caller":"printLogsInDifferentLevel","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"14","column":"10"},"message":"error log"}');
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'verbose',
time: 0,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '13',
column: '10',
},
message: 'verbose log',
},
{
level: 'debug',
time: 1,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '14',
column: '10',
},
message: 'debug log',
},
{
level: 'info',
time: 2,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '15',
column: '10',
},
message: 'info log',
},
{
level: 'warn',
time: 3,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '16',
column: '10',
},
message: 'warn log',
},
{
level: 'error',
time: 4,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentLevel',
fileName: 'test-logs.ts',
path: currentFolder,
line: '17',
column: '10',
},
message: 'error log',
},
]);
});
it('should print session information', async () => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*,-Logger1:Instance2*');
logger1.setLogPattern('Logger1:*,-Logger1:Instance2*');
logger2.setLogPattern('Logger1:*,-Logger1:Instance2*');
logger4_node_1.Logger4Node.Trace.requestHandler(() => ({ key1: 'value1', key2: 'value2' }))({}, {}, async () => {

@@ -263,6 +557,3 @@ await (0, test_logs_1.printLogsInDifferentLevel)(logger1Instance1);

(0, chai_1.expect)(callbackSpy.callCount).to.equal(6);
const calls = new Array(6)
.fill(0)
.map((zero, index) => callbackSpy.getCall(index).args.join(' '))
.map((each) => JSON.parse(each));
const calls = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
calls.forEach((each_) => {

@@ -276,4 +567,4 @@ const each = each_;

time: 0,
pid: 1,
hostname: 'hostname',
extra: {},
stack: '',
className: 'Logger1:Instance1',

@@ -285,3 +576,3 @@ request: { key1: 'value1', key2: 'value2' },

path: currentFolder,
line: '10',
line: '13',
column: '10',

@@ -293,4 +584,4 @@ },

time: 1,
pid: 1,
hostname: 'hostname',
extra: {},
stack: '',
className: 'Logger1:Instance1',

@@ -302,3 +593,3 @@ request: { key1: 'value1', key2: 'value2' },

path: currentFolder,
line: '11',
line: '14',
column: '10',

@@ -310,4 +601,4 @@ },

time: 2,
pid: 1,
hostname: 'hostname',
extra: {},
stack: '',
className: 'Logger1:Instance1',

@@ -319,3 +610,3 @@ request: { key1: 'value1', key2: 'value2' },

path: currentFolder,
line: '12',
line: '15',
column: '10',

@@ -328,4 +619,4 @@ },

time: 3,
pid: 1,
hostname: 'hostname',
extra: {},
stack: '',
className: 'Logger1:Instance1',

@@ -337,3 +628,3 @@ request: { key1: 'value1', key2: 'value2' },

path: currentFolder,
line: '13',
line: '16',
column: '10',

@@ -346,4 +637,4 @@ },

time: 4,
pid: 1,
hostname: 'hostname',
extra: {},
stack: '',
className: 'Logger1:Instance1',

@@ -355,3 +646,3 @@ request: { key1: 'value1', key2: 'value2' },

path: currentFolder,
line: '14',
line: '17',
column: '10',

@@ -364,4 +655,3 @@ },

time: 5,
pid: 1,
hostname: 'hostname',
stack: '',
className: 'Logger1:Instance1',

@@ -374,3 +664,3 @@ request: { key1: 'value1', key2: 'value2' },

path: currentFolder,
line: '19',
line: '22',
column: '10',

@@ -383,3 +673,3 @@ },

callbackSpy.restore();
loggerSpy.reset();
test_logs_1.loggerSpy.reset();
});

@@ -403,5 +693,8 @@ });

beforeEach(() => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*,Logger2:*');
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger4_node_1.Logger4Node.setLogSeverityPattern(logSeverity, undefined));
logger1.setLogPattern('Logger1:*,Logger2:*');
logger2.setLogPattern('Logger1:*,Logger2:*');
logger1.setLogLevel(logger_1.LogSeverity.VERBOSE);
logger2.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger1.setLogSeverityPattern(logSeverity, undefined));
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger2.setLogSeverityPattern(logSeverity, undefined));
callbackSpy = sinon_1.default.spy(console, 'log');

@@ -411,37 +704,74 @@ });

await (0, test_logs_1.printLogsInDifferentType)(logger1Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"error","time":0,"pid":1,"hostname":"hostname","className":"Logger1:Instance1","source"'
+ ':{"caller":"printLogsInDifferentType","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"24","column":"10"},"message":"this is 1 true {\\\\\\"key1\\\\\\":1,\\\\\\"value\\\\\\":2}"}');
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'error',
time: 0,
extra: {},
stack: '',
className: 'Logger1:Instance1',
source: {
caller: 'printLogsInDifferentType',
fileName: 'test-logs.ts',
path: currentFolder,
line: '27',
column: '10',
},
message: 'this is 1 true {"key1":1,"value":2}',
},
]);
});
it('should print logs only in string', async () => {
await (0, test_logs_1.printLogsInDifferentType)(logger2Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"error","time":0,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source"'
+ ':{"caller":"printLogsInDifferentType","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"24","column":"10"},"message":"this is 1 true {\\\\\\"key1\\\\\\":1,\\\\\\"value\\\\\\":2}"}');
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'error',
time: 0,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printLogsInDifferentType',
fileName: 'test-logs.ts',
path: currentFolder,
line: '27',
column: '10',
},
message: 'this is 1 true {"key1":1,"value":2}',
},
]);
});
it('should print logs only in string for fatal', async () => {
await (0, test_logs_1.printFatalLogsInDifferentType)(logger2Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('{"level":"fatal","time":0,"pid":1,"hostname":"hostname","className":"Logger2:Instance1","source"'
+ ':{"caller":"printFatalLogsInDifferentType","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"29","column":"10"},"message":"this is 1 true {\\\\\\"key1\\\\\\":1,\\\\\\"value\\\\\\":2}"}');
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'fatal',
time: 0,
extra: {},
stack: '',
className: 'Logger2:Instance1',
source: {
caller: 'printFatalLogsInDifferentType',
fileName: 'test-logs.ts',
path: currentFolder,
line: '32',
column: '10',
},
message: 'this is 1 true {"key1":1,"value":2}',
},
]);
});
afterEach(() => {
callbackSpy.restore();
loggerSpy.reset();
test_logs_1.loggerSpy.reset();
});
});
context('logging string, object, array in one log', () => {
let logger;
let callbackSpy;
let loggerInstance;
before(() => {
const logger = new logger4_node_1.Logger4Node('Logger');
logger = new logger4_node_1.Logger4Node('Logger');
loggerInstance = logger.instance('Instance');

@@ -451,102 +781,69 @@ logger.setJsonLogging(true);

beforeEach(() => {
logger4_node_1.Logger4Node.setLogPattern('Logger:*');
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger4_node_1.Logger4Node.setLogSeverityPattern(logSeverity, undefined));
logger.setLogPattern('Logger:*');
logger.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger.setLogSeverityPattern(logSeverity, undefined));
callbackSpy = sinon_1.default.spy(console, 'log');
});
it('should log object with string in proper json format', async () => {
loggerInstance.error('this is string', { var: 1, var2: 2 });
await (0, test_logs_1.wait)();
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args[0]).to
.equal('{"level":"error","time":0,"pid":1,"hostname":"hostname","className":"Logger:Instance","source"'
+ ':{"caller":"Context.<anonymous>","fileName":"test-json.spec.ts","path":"'
+ `${currentFolder}`
+ '","line":"447","column":"22"},"message":"this is string {\\\\\\"var\\\\\\":1,\\\\\\"var2\\\\\\":2}"}');
(0, chai_1.expect)(JSON.parse(callbackSpy.getCall(0).args[0])).to.deep.equal({
level: 'error',
time: 0,
pid: 1,
hostname: 'hostname',
className: 'Logger:Instance',
source: {
caller: 'Context.<anonymous>',
fileName: 'test-json.spec.ts',
path: currentFolder,
line: '447',
column: '22',
},
message: 'this is string {\\"var\\":1,\\"var2\\":2}',
});
});
it('should log multi line string in one line', async () => {
await (0, test_logs_1.printLogWithMultipleEndCharacters)(loggerInstance);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args[0]).to
.equal('{"level":"error","time":0,"pid":1,"hostname":"hostname","className":"Logger:Instance","source"'
+ ':{"caller":"printLogWithMultipleEndCharacters","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"34","column":"10"},"message":"this is line1\\\\nline2\\\\nline2 {\\\\\\"var\\\\\\":1,\\\\\\"var2\\\\\\":2}"}');
(0, chai_1.expect)(JSON.parse(callbackSpy.getCall(0).args[0])).to.deep.equal({
level: 'error',
time: 0,
pid: 1,
hostname: 'hostname',
className: 'Logger:Instance',
source: {
caller: 'printLogWithMultipleEndCharacters',
fileName: 'test-logs.ts',
path: currentFolder,
line: '34',
column: '10',
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'error',
time: 0,
extra: {},
stack: '',
className: 'Logger:Instance',
source: {
caller: 'printLogWithMultipleEndCharacters',
fileName: 'test-logs.ts',
path: currentFolder,
line: '37',
column: '10',
},
message: 'this is line1\nline2\nline2 {"var":1,"var2":2}',
},
message: 'this is line1\\nline2\\nline2 {\\"var\\":1,\\"var2\\":2}',
});
]);
});
it('should log properly when message contains \\"', async () => {
await (0, test_logs_1.printLogWithBackSlashCharacter)(loggerInstance);
(0, chai_1.expect)(callbackSpy.getCall(0).args[0]).to
.equal('{"level":"error","time":0,"pid":1,"hostname":"hostname","className":"Logger:Instance","source"'
+ ':{"caller":"printLogWithBackSlashCharacter","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"39","column":"10"},"message":"this is line1 \\\\\\\\\\\\\\" {\\\\\\"var\\\\\\":1,\\\\\\"var2\\\\\\":2}"}');
(0, chai_1.expect)(JSON.parse(callbackSpy.getCall(0).args[0])).to.deep.equal({
level: 'error',
time: 0,
pid: 1,
hostname: 'hostname',
className: 'Logger:Instance',
source: {
caller: 'printLogWithBackSlashCharacter',
fileName: 'test-logs.ts',
path: currentFolder,
line: '39',
column: '10',
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'error',
time: 0,
extra: {},
stack: '',
className: 'Logger:Instance',
source: {
caller: 'printLogWithBackSlashCharacter',
fileName: 'test-logs.ts',
path: currentFolder,
line: '42',
column: '10',
},
message: 'this is line1 \\" {"var":1,"var2":2}',
},
message: 'this is line1 \\\\\\" {\\"var\\":1,\\"var2\\":2}',
});
]);
});
it('should log properly when message contains \t', async () => {
await (0, test_logs_1.printLogWithSpecialTabCharacter)(loggerInstance);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args[0]).to
.equal('{"level":"error","time":0,"pid":1,"hostname":"hostname","className":"Logger:Instance","source"'
+ ':{"caller":"printLogWithSpecialTabCharacter","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"44","column":"10"},"message":"this is line1 \\\\t"}');
(0, chai_1.expect)(JSON.parse(callbackSpy.getCall(0).args[0])).to.deep.equal({
level: 'error',
time: 0,
pid: 1,
hostname: 'hostname',
className: 'Logger:Instance',
source: {
caller: 'printLogWithSpecialTabCharacter',
fileName: 'test-logs.ts',
path: currentFolder,
line: '44',
column: '10',
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'error',
time: 0,
extra: {},
stack: '',
className: 'Logger:Instance',
source: {
caller: 'printLogWithSpecialTabCharacter',
fileName: 'test-logs.ts',
path: currentFolder,
line: '47',
column: '10',
},
message: 'this is line1 \t',
},
message: 'this is line1 \\t',
});
]);
});

@@ -560,6 +857,7 @@ it('should log properly when message contains new line character with \n', async () => {

callbackSpy.restore();
loggerSpy.reset();
test_logs_1.loggerSpy.reset();
});
});
context('github link logging', () => {
let logger;
let callbackSpy;

@@ -569,3 +867,3 @@ let loggerInstance;

const currentPathSplit = __dirname.split('/');
const logger = new logger4_node_1.Logger4Node('Logger', {
logger = new logger4_node_1.Logger4Node('Logger', {
github: {

@@ -582,5 +880,5 @@ basePath: currentPathSplit.slice(0, currentPathSplit.length - 1).join('/'),

beforeEach(() => {
logger4_node_1.Logger4Node.setLogPattern('Logger:*');
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger4_node_1.Logger4Node.setLogSeverityPattern(logSeverity, undefined));
logger.setLogPattern('Logger:*');
logger.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger.setLogSeverityPattern(logSeverity, undefined));
callbackSpy = sinon_1.default.spy(console, 'log');

@@ -590,29 +888,25 @@ });

await (0, test_logs_1.printLogSingleLine)(loggerInstance);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args[0]).to
.equal('{"level":"error","time":0,"pid":1,"hostname":"hostname","className":"Logger:Instance","source"'
+ ':{"caller":"printLogSingleLine","fileName":"test-logs.ts","path":"'
+ `${currentFolder}`
+ '","line":"49","column":"10","github":"https://github.com/yog27ray/logger4node/blob/fd4a2de07ed9e31d890370e05fb4b8a416f27224'
+ '/spec/test-logs.ts#L49"},"message":"this is string"}');
(0, chai_1.expect)(JSON.parse(callbackSpy.getCall(0).args[0])).to.deep.equal({
level: 'error',
time: 0,
pid: 1,
hostname: 'hostname',
className: 'Logger:Instance',
source: {
caller: 'printLogSingleLine',
fileName: 'test-logs.ts',
path: currentFolder,
line: '49',
column: '10',
github: 'https://github.com/yog27ray/logger4node/blob/fd4a2de07ed9e31d890370e05fb4b8a416f27224/spec/test-logs.ts#L49',
const logs = (0, test_logs_1.stringLogsToJSON)(callbackSpy);
(0, chai_1.expect)(logs).to.deep.equal([
{
level: 'error',
time: 0,
extra: {},
stack: '',
className: 'Logger:Instance',
source: {
caller: 'printLogSingleLine',
fileName: 'test-logs.ts',
path: currentFolder,
line: '52',
column: '10',
github: 'https://github.com/yog27ray/logger4node/blob/fd4a2de07ed9e31d890370e05fb4b8a416f27224/spec/test-logs.ts#L52',
},
message: 'this is string',
},
message: 'this is string',
});
]);
});
afterEach(() => {
callbackSpy.restore();
loggerSpy.reset();
test_logs_1.loggerSpy.reset();
});

@@ -619,0 +913,0 @@ });

@@ -0,1 +1,2 @@

import { SinonSpy } from 'sinon';
import { Logger } from '../src/logger/logger';

@@ -12,1 +13,7 @@ export declare function wait(time?: number): Promise<void>;

export declare function printLogWithNewLineAndSlashNCharacter(logger: Logger): Promise<void>;
declare const loggerSpy: {
log(_data: string): void;
reset(): void;
};
export declare function stringLogsToJSON(spy: SinonSpy): Array<Record<string, unknown>>;
export { loggerSpy };
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printLogWithNewLineAndSlashNCharacter = exports.printLogSingleLine = exports.printLogWithSpecialTabCharacter = exports.printLogWithBackSlashCharacter = exports.printLogWithMultipleEndCharacters = exports.printFatalLogsInDifferentType = exports.printLogsInDifferentType = exports.printLogsWithExtraFields = exports.printLogsInDifferentLevel = exports.wait = void 0;
exports.loggerSpy = exports.stringLogsToJSON = exports.printLogWithNewLineAndSlashNCharacter = exports.printLogSingleLine = exports.printLogWithSpecialTabCharacter = exports.printLogWithBackSlashCharacter = exports.printLogWithMultipleEndCharacters = exports.printFatalLogsInDifferentType = exports.printLogsInDifferentType = exports.printLogsWithExtraFields = exports.printLogsInDifferentLevel = exports.wait = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const tail_1 = require("tail");
const logger_1 = require("../src/logger/logger");

@@ -72,2 +77,30 @@ function wait(time = 100) {

exports.printLogWithNewLineAndSlashNCharacter = printLogWithNewLineAndSlashNCharacter;
const spyConsoleLog = [];
const loggerSpy = {
log(_data) {
let data = _data;
const json = JSON.parse(data);
json.time = spyConsoleLog.length;
data = JSON.stringify(json);
console.log(JSON.stringify(json));
spyConsoleLog.push(data);
},
reset() {
spyConsoleLog.splice(0, spyConsoleLog.length);
},
};
exports.loggerSpy = loggerSpy;
node_fs_1.default.writeFileSync('./spec/test.logs', '', 'utf-8');
new tail_1.Tail('./spec/test.logs')
.on('line', (data) => loggerSpy.log(data))
.on('error', (error) => console.log(error))
.watch();
function stringLogsToJSON(spy) {
return new Array(spy.callCount).fill(0).map((zero, index) => {
const jsonLog = JSON.parse(spy.getCall(index).args.join(' '));
jsonLog.time = index;
return jsonLog;
});
}
exports.stringLogsToJSON = stringLogsToJSON;
//# sourceMappingURL=test-logs.js.map

@@ -36,5 +36,8 @@ "use strict";

beforeEach(() => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*');
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger4_node_1.Logger4Node.setLogSeverityPattern(logSeverity, undefined));
logger1.setLogPattern('Logger1:*');
logger1.setLogLevel(logger_1.LogSeverity.VERBOSE);
logger2.setLogPattern('Logger1:*');
logger2.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger1.setLogSeverityPattern(logSeverity, undefined));
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger2.setLogSeverityPattern(logSeverity, undefined));
callbackSpy = sinon_1.default.spy(console, 'log');

@@ -56,3 +59,4 @@ });

it('should allow print logger2 logs', () => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*,Logger2:*');
logger1.setLogPattern('Logger1:*,Logger2:*');
logger2.setLogPattern('Logger1:*,Logger2:*');
printLogsInDifferentLevel(logger2Instance1);

@@ -67,3 +71,4 @@ (0, chai_1.expect)(callbackSpy.callCount).to.equal(5);

it('should print only Logger1 Debug and above logs', () => {
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.WARN);
logger1.setLogLevel(logger_1.LogSeverity.WARN);
logger2.setLogLevel(logger_1.LogSeverity.WARN);
printLogsInDifferentLevel(logger1Instance1);

@@ -76,4 +81,6 @@ printLogsInDifferentLevel(logger2Instance1);

it('should print only Logger1 Debug and above logs and logger2 only Debug: ', () => {
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.WARN);
logger4_node_1.Logger4Node.setLogSeverityPattern(logger_1.LogSeverity.WARN, 'Logger2:*');
logger1.setLogLevel(logger_1.LogSeverity.WARN);
logger2.setLogLevel(logger_1.LogSeverity.WARN);
logger1.setLogSeverityPattern(logger_1.LogSeverity.WARN, 'Logger2:*');
logger2.setLogSeverityPattern(logger_1.LogSeverity.WARN, 'Logger2:*');
printLogsInDifferentLevel(logger1Instance1);

@@ -102,3 +109,4 @@ printLogsInDifferentLevel(logger2Instance1);

it('should print only instance1 of Logger1', () => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*,-Logger1:Instance2*');
logger1.setLogPattern('Logger1:*,-Logger1:Instance2*');
logger2.setLogPattern('Logger1:*,-Logger1:Instance2*');
printLogsInDifferentLevel(logger1Instance1);

@@ -131,13 +139,10 @@ printLogsInDifferentLevel(logger1Instance2);

beforeEach(() => {
logger4_node_1.Logger4Node.setLogPattern('Logger1:*,Logger2:*');
logger4_node_1.Logger4Node.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger4_node_1.Logger4Node.setLogSeverityPattern(logSeverity, undefined));
logger1.setLogPattern('Logger1:*,Logger2:*');
logger2.setLogPattern('Logger1:*,Logger2:*');
logger1.setLogLevel(logger_1.LogSeverity.VERBOSE);
logger2.setLogLevel(logger_1.LogSeverity.VERBOSE);
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger1.setLogSeverityPattern(logSeverity, undefined));
Object.keys(logger_1.LogLevel).forEach((logSeverity) => logger2.setLogSeverityPattern(logSeverity, undefined));
callbackSpy = sinon_1.default.spy(console, 'log');
});
it('should print logs not only in string', () => {
printLogsInDifferentType(logger1Instance1);
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1);
(0, chai_1.expect)(callbackSpy.getCall(0).args.join(' ')).to
.equal('Error: Logger1:Instance1 this is 1 true { key1: 1, value: 2 }');
});
it('should print logs only in string', () => {

@@ -144,0 +149,0 @@ printLogsInDifferentType(logger2Instance1);

@@ -21,8 +21,16 @@ export declare interface GithubConfig {

};
export declare function setLogLevel(logSeverity: LogSeverity): void;
export declare function setLogPattern(pattern: string): void;
export declare function setLogSeverityPattern(level: LogSeverity, pattern: string): void;
export declare interface LogPattern {
negative: Array<string>;
positive: Array<string>;
}
export declare type LogSeverityPattern = {
[key in LogSeverity]: LogPattern;
};
export declare function setLogPattern(logPattern: LogPattern, pattern: string): void;
export declare function setLogSeverityPattern(logSeverityPattern: LogSeverityPattern, level: LogSeverity, pattern: string): void;
declare interface LoggerConfig {
github: GithubConfig;
stringLogging(): boolean;
logSeverityPattern: LogSeverityPattern;
logPattern: LogPattern;
minLogLevelEnabled(): number;
jsonLogging(): boolean;

@@ -35,3 +43,3 @@ }

private static jsonTransformArgs;
private static handleJSONSpecialCharacter;
private static transformArgs;
verbose(formatter: unknown, ...args: Array<unknown>): void;

@@ -46,3 +54,2 @@ info(formatter: unknown, ...args: Array<unknown>): void;

private generateLogSource;
private transformArgs;
private isLogEnabled;

@@ -49,0 +56,0 @@ private generateGithubLink;

@@ -6,5 +6,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.setLogSeverityPattern = exports.setLogPattern = exports.setLogLevel = exports.DisplaySeverityMap = exports.LogLevel = exports.LogSeverity = void 0;
exports.Logger = exports.setLogSeverityPattern = exports.setLogPattern = exports.DisplaySeverityMap = exports.LogLevel = exports.LogSeverity = void 0;
const util_1 = __importDefault(require("util"));
const pino_logger_1 = require("../pino/pino.logger");
const trace_1 = require("../trace/trace");

@@ -37,8 +36,5 @@ var LogSeverity;

const currentFolder = __dirname;
// const ignoreFolders = [
// `${process.cwd()}/src/logger.ts`,
// `${process.cwd()}/src/logger4-node.ts`,
// `${process.cwd()}/node_modules/src/logger.ts`,
// `${process.cwd()}/node_modules/src/logger4-node.ts`,
// ];
function stringify(data) {
return JSON.stringify(data);
}
function generateMatchAndDoesNotMatchArray(input = '') {

@@ -67,13 +63,2 @@ const positive = [];

}
let positive = [];
let negative = [];
let minLogLevelEnabled = exports.LogLevel.debug;
const LOG_PATTERN = {
[LogSeverity.VERBOSE]: { positive: [], negative: [] },
[LogSeverity.INFO]: { positive: [], negative: [] },
[LogSeverity.WARN]: { positive: [], negative: [] },
[LogSeverity.DEBUG]: { positive: [], negative: [] },
[LogSeverity.ERROR]: { positive: [], negative: [] },
[LogSeverity.FATAL]: { positive: [], negative: [] },
};
function isNotMatchWithPatterns(patterns, value) {

@@ -85,12 +70,16 @@ return patterns.every((pattern) => !new RegExp(`^${pattern}$`).test(value));

}
function setLogLevel(logSeverity) {
minLogLevelEnabled = exports.LogLevel[logSeverity] || exports.LogLevel[LogSeverity.DEBUG];
function setLogPattern(logPattern, pattern) {
logPattern.positive.splice(0, logPattern.positive.length);
logPattern.negative.splice(0, logPattern.positive.length);
const [positive, negative] = generateMatchAndDoesNotMatchArray(pattern);
logPattern.positive.push(...positive);
logPattern.negative.push(...negative);
}
exports.setLogLevel = setLogLevel;
function setLogPattern(pattern) {
([positive, negative] = generateMatchAndDoesNotMatchArray(pattern));
}
exports.setLogPattern = setLogPattern;
function setLogSeverityPattern(level, pattern) {
([LOG_PATTERN[level].positive, LOG_PATTERN[level].negative] = pattern ? generateMatchAndDoesNotMatchArray(pattern) : [[], []]);
function setLogSeverityPattern(logSeverityPattern, level, pattern) {
logSeverityPattern[level].positive.splice(0, logSeverityPattern[level].positive.length);
logSeverityPattern[level].negative.splice(0, logSeverityPattern[level].positive.length);
const [positive, negative] = pattern ? generateMatchAndDoesNotMatchArray(pattern) : [[], []];
logSeverityPattern[level].positive.push(...positive);
logSeverityPattern[level].negative.push(...negative);
}

@@ -104,22 +93,24 @@ exports.setLogSeverityPattern = setLogSeverityPattern;

if (!errorStacks.length) {
return undefined;
return '';
}
return this.handleJSONSpecialCharacter(errorStacks.join('\\n|\\n'));
return errorStacks.join('\\n|\\n');
}
static jsonTransformArgs(...args) {
const message = util_1.default.format(...args.map((each) => {
if (['string', 'number', 'boolean', 'bigint', 'function', 'undefined'].includes(typeof each)) {
return util_1.default.format(...args.map((each) => {
if (['string', 'number', 'boolean', 'bigint', 'function'].includes(typeof each)) {
return each;
}
return JSON.stringify(each);
return stringify(each);
}));
return this.handleJSONSpecialCharacter(message);
}
static handleJSONSpecialCharacter(message) {
return message
.replace(/\\/g, '\\\\')
.replace(/\t/g, '\\t')
.replace(/"/g, '\\"')
.replace(/\r\n/g, '\\r\\n')
.replace(/\n/g, '\\n');
static transformArgs(...args) {
return args.map((each) => {
if (['string', 'number', 'boolean', 'bigint', 'function', 'undefined'].includes(typeof each)) {
return each;
}
if (each instanceof Error) {
return each;
}
return stringify(each);
});
}

@@ -153,15 +144,15 @@ verbose(formatter, ...args) {

if (this.config.jsonLogging()) {
const source = this.generateLogSource();
const logMessage = Logger.jsonTransformArgs(formatter, ...args);
pino_logger_1.pinoLogger.fatal({
console.log(stringify({
level: logSeverity,
time: new Date().toISOString(),
className: this.name,
level: logSeverity,
source: this.generateLogSource(),
message: Logger.jsonTransformArgs(formatter, ...args),
request: trace_1.Trace.getRequestInfo(),
extra: extraData,
extra: extraData || {},
stack: Logger.errorStack(formatter, ...args),
source,
}, logMessage);
}));
return;
}
console.log(`${exports.DisplaySeverityMap[logSeverity]}:`, this.name, util_1.default.format(formatter, ...this.transformArgs(...args)));
console.log(`${exports.DisplaySeverityMap[logSeverity]}:`, this.name, util_1.default.format(formatter, ...Logger.transformArgs(...args)));
}

@@ -175,3 +166,3 @@ generateLogSource() {

if (!logSource) {
return undefined;
return {};
}

@@ -181,3 +172,3 @@ if (logSource[logSource.length - 1] === ')') {

if (!filePath) {
return undefined;
return {};
}

@@ -187,3 +178,3 @@ const filePathSplit = filePath.substring(0, filePath.length - 1).split('/');

if (!fileName || !line || !column) {
return undefined;
return {};
}

@@ -203,3 +194,3 @@ const path = filePathSplit.join('/');

if (!fileName || !line || !column) {
return undefined;
return {};
}

@@ -215,30 +206,16 @@ const path = filePathSplit.join('/');

}
transformArgs(...args) {
return args.map((each) => {
if (!this.config.stringLogging()) {
return each;
}
if (['string', 'number', 'boolean', 'bigint', 'function', 'undefined'].includes(typeof each)) {
return each;
}
if (each instanceof Error) {
return each;
}
return JSON.stringify(each);
});
}
isLogEnabled(logSeverity) {
if (!isNotMatchWithPatterns(LOG_PATTERN[logSeverity].negative, this.name)) {
if (!isNotMatchWithPatterns(this.config.logSeverityPattern[logSeverity].negative, this.name)) {
return false;
}
if (isMatchWithPatterns(LOG_PATTERN[logSeverity].positive, this.name)) {
if (isMatchWithPatterns(this.config.logSeverityPattern[logSeverity].positive, this.name)) {
return true;
}
if (exports.LogLevel[logSeverity] < minLogLevelEnabled) {
if (exports.LogLevel[logSeverity] < this.config.minLogLevelEnabled()) {
return false;
}
if (!isNotMatchWithPatterns(negative, this.name)) {
if (!isNotMatchWithPatterns(this.config.logPattern.negative, this.name)) {
return false;
}
return isMatchWithPatterns(positive, this.name);
return isMatchWithPatterns(this.config.logPattern.positive, this.name);
}

@@ -245,0 +222,0 @@ generateGithubLink(file, line) {

@@ -8,6 +8,9 @@ import { Trace } from '../trace/trace';

private jsonLogging;
private minLogLevelEnabled;
private readonly github;
static setLogLevel(logSeverity: LogSeverity): void;
static setLogPattern(pattern: string): void;
static setLogSeverityPattern(level: LogSeverity, pattern: string): void;
private readonly logSeverityPattern;
private readonly logPattern;
setLogLevel(logSeverity?: LogSeverity): void;
setLogPattern(pattern?: string): void;
setLogSeverityPattern(level: LogSeverity, pattern?: string): void;
setStringLogging(stringOnly: boolean): void;

@@ -14,0 +17,0 @@ setJsonLogging(jsonLogging: boolean): void;

@@ -7,10 +7,10 @@ "use strict";

class Logger4Node {
static setLogLevel(logSeverity) {
(0, logger_1.setLogLevel)(logSeverity);
setLogLevel(logSeverity = process.env.DEBUG_LEVEL) {
this.minLogLevelEnabled = logger_1.LogLevel[logSeverity] || logger_1.LogLevel[logger_1.LogSeverity.DEBUG];
}
static setLogPattern(pattern) {
(0, logger_1.setLogPattern)(pattern);
setLogPattern(pattern = process.env.DEBUG) {
(0, logger_1.setLogPattern)(this.logPattern, pattern);
}
static setLogSeverityPattern(level, pattern) {
(0, logger_1.setLogSeverityPattern)(level, pattern);
setLogSeverityPattern(level, pattern) {
(0, logger_1.setLogSeverityPattern)(this.logSeverityPattern, level, pattern || process.env[`LOG_${level.toUpperCase()}`]);
}

@@ -26,4 +26,18 @@ setStringLogging(stringOnly) {

this.jsonLogging = false;
this.minLogLevelEnabled = logger_1.LogLevel[logger_1.LogSeverity.DEBUG];
this.logSeverityPattern = {
[logger_1.LogSeverity.FATAL]: { positive: [], negative: [] },
[logger_1.LogSeverity.ERROR]: { positive: [], negative: [] },
[logger_1.LogSeverity.WARN]: { positive: [], negative: [] },
[logger_1.LogSeverity.INFO]: { positive: [], negative: [] },
[logger_1.LogSeverity.DEBUG]: { positive: [], negative: [] },
[logger_1.LogSeverity.VERBOSE]: { positive: [], negative: [] },
};
this.logPattern = { positive: [], negative: [] };
this._applicationName = applicationName;
this.github = option.github ? { ...option.github } : undefined;
this.setLogLevel(process.env.DEBUG_LEVEL);
this.setLogPattern(process.env.DEBUG);
Object.keys(logger_1.LogLevel)
.forEach((logSeverity) => this.setLogSeverityPattern(logSeverity, process.env[`LOG_${logSeverity.toUpperCase()}`]));
}

@@ -33,4 +47,6 @@ instance(name) {

github: this.github,
logSeverityPattern: this.logSeverityPattern,
logPattern: this.logPattern,
minLogLevelEnabled: () => this.minLogLevelEnabled,
jsonLogging: () => this.jsonLogging,
stringLogging: () => this.stringLogging,
});

@@ -41,6 +57,2 @@ }

Logger4Node.Trace = trace_1.Trace;
Logger4Node.setLogLevel(process.env.DEBUG_LEVEL);
Logger4Node.setLogPattern(process.env.DEBUG);
Object.keys(logger_1.LogLevel)
.forEach((logSeverity) => Logger4Node.setLogSeverityPattern(logSeverity, process.env[`LOG_${logSeverity.toUpperCase()}`]));
//# sourceMappingURL=logger4-node.js.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc