Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/wrlgs 12 dep upgrades #169

Merged
merged 11 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
run: sudo apt-get update -q
- uses: actions/setup-node@v4
with:
node-version: '16'
node-version: '20'
- name: Install Yarn
run: npm install -g yarn
- name: install dependencies
Expand Down
4 changes: 2 additions & 2 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ might have already lost quite a bit of priceless information about the error
encountered, and the code path the request went through. To address this, we
offer multiple features:

* [Request ID namespacing](###request-id-namespacing)
* [Request unit Logs](###request-unit-logs)
* [Request ID namespacing](###Request ID namespacing)
* [Request unit Logs](###Request unit Logs)

### Request ID namespacing

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# WereLogs

[![Scality CI][badgepriv]](http://ci.ironmann.io/gh/scality/werelogs)

This repository provides a NodeJS Library that aims to be an efficient logging
library, reducing as much as possible the need to compute anything in NodeJS,
and focusing on a simple I/O scheme. The goal here is to make the most of
Expand Down
19 changes: 19 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default [...compat.extends("scality"), {
languageOptions: {
ecmaVersion: 2020,
sourceType: "script",
},
}];
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const API = require('./lib/api.js');
const API = require('./lib/api');
const stderrUtils = require('./lib/stderrUtils');

/*
Expand Down
7 changes: 2 additions & 5 deletions lib/Config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

// eslint-disable-line strict

const LogLevel = require('./LogLevel.js');
const SimpleLogger = require('./SimpleLogger.js');
const LogLevel = require('./LogLevel');
const SimpleLogger = require('./SimpleLogger');
/**
* This class is the central point of the once-only configuration system for
* werelogs. It is instanciated once to be exported, and all configuration
Expand Down
3 changes: 0 additions & 3 deletions lib/LogLevel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

// eslint-disable-line strict

const logLevels = [
'trace',
'debug',
Expand Down
32 changes: 18 additions & 14 deletions lib/Logger.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

// eslint-disable-line strict

const LogLevel = require('./LogLevel.js');
const RequestLogger = require('./RequestLogger.js');
const { unserializeUids } = require('./Utils.js');
const Config = require('./Config.js');
const LogLevel = require('./LogLevel');
const RequestLogger = require('./RequestLogger');
const { unserializeUids } = require('./Utils');
const Config = require('./Config');

class Logger {
/**
Expand Down Expand Up @@ -38,9 +35,13 @@ class Logger {
* @returns {RequestLogger} A Valid Request Logger
*/
newRequestLogger(uids) {
const rLog = new RequestLogger(this.config.logger,
this.config.level, this.config.dump,
this.config.end, uids);
const rLog = new RequestLogger(
this.config.logger,
this.config.level,
this.config.dump,
this.config.end,
uids,
);
rLog.addDefaultFields({ name: this.name });
return rLog;
}
Expand All @@ -54,10 +55,13 @@ class Logger {
* @returns {RequestLogger} A Valid Request Logger
*/
newRequestLoggerFromSerializedUids(serializedUids) {
const rLog = new RequestLogger(this.config.logger,
this.config.level, this.config.dump,
const rLog = new RequestLogger(
this.config.logger,
this.config.level,
this.config.dump,
this.config.end,
unserializeUids(serializedUids));
unserializeUids(serializedUids),
);
rLog.addDefaultFields({ name: this.name });
return rLog;
}
Expand All @@ -84,7 +88,7 @@ class Logger {
});
}
const args = [finalData, msg];
sLogger[levelName].apply(sLogger, args);
sLogger[levelName](...args);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions lib/RequestLogger.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

// eslint-disable-line strict

const LogLevel = require('./LogLevel.js');
const Utils = require('./Utils.js');
const LogLevel = require('./LogLevel');
const Utils = require('./Utils');

const { serializeUids, generateUid, objectCopy } = Utils;

Expand Down
6 changes: 2 additions & 4 deletions lib/SimpleLogger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

// eslint-disable-line strict

const os = require('os');
const safeJSONStringify = require('safe-json-stringify');
const fastJSONStringify = require('fast-safe-stringify')
const fastJSONStringify = require('fast-safe-stringify');

/*
* This function safely stringifies JSON. If an exception occcurs (due to
Expand All @@ -18,6 +15,7 @@ function safeStringify(obj) {
try {
// Try to stringify the object (fast version)
str = fastJSONStringify(obj);
// eslint-disable-next-line no-unused-vars
} catch (e) {
// fallback to remove circular object references or other exceptions
// eslint-disable-next-line no-param-reassign
Expand Down
9 changes: 3 additions & 6 deletions lib/Utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@

// eslint-disable-line strict

/**
* @constant
* @type {String[]} - The lookup table to generate the UID
*/
const lut = [];
for (let i = 0; i < 256; i++) {
for (let i = 0; i < 256; i += 1) {
lut[i] = (i < 16 ? '0' : '') + (i).toString(16);
}

Expand Down Expand Up @@ -71,11 +68,11 @@ function objectCopy(target) {
const result = target;
/* eslint-disable prefer-rest-params */
const nb = arguments.length;
for (let i = 1; i < nb; i++) {
for (let i = 1; i < nb; i += 1) {
const source = arguments[i];
const keys = Object.keys(source);
const keysNb = keys.length;
for (let j = 0; j < keysNb; j++) {
for (let j = 0; j < keysNb; j += 1) {
const key = keys[j];
result[key] = source[key];
}
Expand Down
7 changes: 2 additions & 5 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

// eslint-disable-line strict

const Config = require('./Config.js');
const Logger = require('./Logger.js');
const Config = require('./Config');
const Logger = require('./Logger');

class API {
/**
Expand Down
12 changes: 3 additions & 9 deletions lib/stderrUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const defaultTimestamp = () => new Date().toISOString();
* @param {string} [date=`defaultTimestamp()`] Date to print
* @returns {boolean} see process.stderr.write
*/
function printErrorWithTimestamp(
err, origin, date = defaultTimestamp(),
) {
function printErrorWithTimestamp(err, origin, date = defaultTimestamp()) {
return process.stderr.write(`${date}: ${origin}:\n${err.stack}\n`);
}

Expand All @@ -42,9 +40,7 @@ function printErrorWithTimestamp(
* will be called with this value
* @returns {undefined}
*/
function catchAndTimestampUncaughtException(
dateFct = defaultTimestamp, exitCode = 1,
) {
function catchAndTimestampUncaughtException(dateFct = defaultTimestamp, exitCode = 1) {
process.on('uncaughtException', (err, origin) => {
printErrorWithTimestamp(err, origin, dateFct());
if (exitCode !== null) {
Expand Down Expand Up @@ -90,9 +86,7 @@ function catchAndTimestampWarning(dateFct = defaultTimestamp) {
* will be called with this value
* @returns {undefined}
*/
function catchAndTimestampStderr(
dateFct = defaultTimestamp, exitCode = 1,
) {
function catchAndTimestampStderr(dateFct = defaultTimestamp, exitCode = 1) {
catchAndTimestampUncaughtException(dateFct, exitCode);
catchAndTimestampWarning(dateFct);
}
Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "werelogs",
"engines": {
"node": ">=10"
"node": ">=20"
},
"version": "8.1.5",
"version": "8.2.0",
"description": "An efficient raw JSON logging library aimed at micro-services architectures.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -37,16 +37,16 @@
"safe-json-stringify": "^1.2.0"
},
"devDependencies": {
"eslint": "^7.32.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-scality": "scality/Guidelines#71a059ad",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.26.0",
"eslint-plugin-react-hooks": "^4.2.0",
"jsdoc": "^3.4.3",
"markdownlint-cli": "^0.27.1",
"mocha": "^8.4.0",
"nyc": "^15.1.0"
"eslint": "^9.9.1",
"eslint-config-scality": "git+https://github.com/scality/Guidelines#8.3.0",
"markdownlint-cli": "^0.41.0",
"mocha": "^10.7.3",
"nyc": "^17.0.0"
},
"resolutions": {
"braces": "^3.0.3",
"markdown-it": "^12.3.2",
"minimatch": "^3.0.5",
"minimist": "^1.2.6"
}
}
16 changes: 7 additions & 9 deletions tests/Utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

// eslint-disable-line strict

const assert = require('assert');

const LogLevel = require('../lib/LogLevel.js');
const LogLevel = require('../lib/LogLevel');

class DummyLogger {
constructor() {
Expand Down Expand Up @@ -69,14 +66,12 @@ function genericFilterGenerator(filterLevel, testLevel, createLogger) {
const logger = createLogger(dummyLogger, filterLevel);

switch (testLevel) {
/* eslint-disable no-multi-spaces */
case 'trace': logger.trace('test trace'); break;
case 'debug': logger.debug('test debug'); break;
case 'info': logger.info('test info'); break;
case 'warn': logger.warn('test warn'); break;
case 'error': logger.error('test error'); break;
case 'fatal': logger.fatal('test fatal'); break;
/* eslint-enable no-multi-spaces */
default:
done(new Error('Unexpected testLevel name: ', testLevel));
}
Expand Down Expand Up @@ -104,13 +99,16 @@ function loggingMisuseGenerator(test, createLogger) {
const logger = createLogger(dummyLogger);
assert.doesNotThrow(
() => {
logger.info.apply(logger, test.args);
logger.info(...test.args);
},
Error,
`Werelogs should not throw with ${test.desc}`,
);
assert(dummyLogger.ops[0][0], 'fatal',
'Expected the Module Logger to have logged a fatal message.');
assert(
dummyLogger.ops[0][0],
'fatal',
'Expected the Module Logger to have logged a fatal message.',
);
done();
};
}
Expand Down
5 changes: 1 addition & 4 deletions tests/functional/basic_usage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@

// eslint-disable-line strict

const assert = require('assert');
const { PassThrough } = require('stream');

const pass = new PassThrough();

const werelogs = require('werelogs'); // eslint-disable-line
const werelogs = require('werelogs');

// With PassThrough, SimpleLogger can use it as Writeable stream and all the
// data being written can be read into a variable
Expand Down
14 changes: 8 additions & 6 deletions tests/functional/multi-modules/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const assert = require('assert');
const { PassThrough } = require('stream');

const Werelogs = require('werelogs'); // eslint-disable-line
const Werelogs = require('werelogs');
const module1 = require('./module1');
const module2 = require('./module2');
const module3 = require('./module3');

const modules = [
require('./module1.js'),
require('./module2.js'),
require('./module3.js'),
module1,
module2,
module3,
];

const pass = new PassThrough();
Expand Down Expand Up @@ -33,7 +37,6 @@ describe('Config is shared and unique within one API', () => {
log.warn('Logging as warn');
const rLog = log.newRequestLogger();
rLog.info('Logging request as info');
/* eslint-disable max-len */
assert.deepStrictEqual(logBuffer.records.length, 5, 'Expected to see 5 log entries in the ring buffer.');
assert.deepStrictEqual(logBuffer.records[0].message, 'Logging as info');
assert.deepStrictEqual(logBuffer.records[0].name, 'test-mod1');
Expand All @@ -51,7 +54,6 @@ describe('Config is shared and unique within one API', () => {
assert.deepStrictEqual(logBuffer.records[4].name, 'test-index');
assert.deepStrictEqual(logBuffer.records[4].level, 'info');
assert.notStrictEqual(logBuffer.records[4].req_id, undefined);
/* eslint-enable max-len */
done();
});
});
3 changes: 2 additions & 1 deletion tests/functional/multi-modules/module1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Werelogs = require('werelogs').Logger; // eslint-disable-line
const Werelogs = require('werelogs').Logger;

const log = new Werelogs('test-mod1');

// eslint-disable-next-line no-redeclare
function test() {
log.info('Logging as info');
}
Expand Down
3 changes: 2 additions & 1 deletion tests/functional/multi-modules/module2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Werelogs = require('werelogs').Logger; // eslint-disable-line
const Werelogs = require('werelogs').Logger;

const log = new Werelogs('test-mod2');

// eslint-disable-next-line no-redeclare
function test() {
log.debug('Logging as debug');
}
Expand Down
Loading