4 Commits
1.1.2 ... 1.1.3

8 changed files with 1762 additions and 2195 deletions
Split View
  1. +7
    -0
      CHANGELOG.md
  2. +2
    -3
      README.md
  3. +11
    -18
      package.json
  4. +59
    -38
      source/index.ts
  5. +9
    -9
      tests/index.test.ts
  6. +52
    -46
      tests/snapshots/index.test.ts.md
  7. BIN
      tests/snapshots/index.test.ts.snap
  8. +1622
    -2081
      yarn.lock

+ 7
- 0
CHANGELOG.md View File

@@ -1,5 +1,12 @@
# Changelog

## [1.1.3](https://git.holllo.cc/Holllo/log/compare/1.1.2...1.1.3)

Released on 2021-07-16.

* Updated the dependencies and tests.
* Made the private methods from the `Log` class protected so it can be extended and made more custom.

## [1.1.2](https://git.holllo.cc/Holllo/log/compare/1.1.1...1.1.2)

Released on 2020-08-04.


+ 2
- 3
README.md View File

@@ -48,11 +48,10 @@ YYYY-MM-DD HH:mm:ss,SSS Debug This shouldn't be in production. 🤔

## Changelog

Latest version ([1.1.2](https://git.holllo.cc/Holllo/log/compare/1.1.1...1.1.2)) changes:
Latest version ([1.1.3](https://git.holllo.cc/Holllo/log/compare/1.1.2...1.1.3)) changes:

* Moved the project from GitLab to self-hosted Gitea instance.
* Updated the dependencies and tests.
* Cleaned up some stuff, no functional changes.
* Made the private methods from the `Log` class protected so it can be extended and made more custom.

The full changelog can be found [here](https://git.holllo.cc/Holllo/log/src/branch/main/CHANGELOG.md).



+ 11
- 18
package.json View File

@@ -1,7 +1,7 @@
{
"name": "@holllo/log",
"description": "Straightforward NodeJS logging.",
"version": "1.1.2",
"version": "1.1.3",
"author": "Holllo <helllo@holllo.cc>",
"homepage": "https://git.holllo.cc/Holllo/log",
"license": "MIT",
@@ -21,20 +21,19 @@
"prepublishOnly": "yarn clean && yarn test && yarn start"
},
"dependencies": {
"chalk": "^4.1.0",
"fecha": "^4.2.0",
"rotating-file-stream": "^2.1.3"
"chalk": "^4.1.1",
"fecha": "^4.2.1",
"rotating-file-stream": "^2.1.5"
},
"devDependencies": {
"@types/sinon": "^9.0.4",
"ava": "^3.11.1",
"husky": "^4.2.5",
"@types/sinon": "^10.0.2",
"ava": "^3.15.0",
"nyc": "^15.1.0",
"sinon": "^9.0.2",
"trash-cli": "^3.0.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.7",
"xo": "^0.32.1"
"sinon": "^11.1.1",
"trash-cli": "^4.0.0",
"ts-node": "^10.1.0",
"typescript": "^4.3.5",
"xo": "0.39.1"
},
"ava": {
"extensions": [
@@ -60,12 +59,6 @@
},
"space": true
},
"husky": {
"hooks": {
"pre-commit": "yarn test",
"pre-push": "yarn test"
}
},
"keywords": [
"console",
"log",


+ 59
- 38
source/index.ts View File

@@ -62,11 +62,10 @@ export default class Log implements LogOptions {

// Set the optional options second.
// Check each one for undefined and if not, set appropriately.
if (typeof options.console === 'undefined') {
this.console = process.env.NODE_ENV === 'development';
} else {
this.console = options.console;
}
this.console =
typeof options.console === 'undefined'
? process.env.NODE_ENV === 'development'
: options.console;

if (typeof options.level === 'undefined') {
this.level =
@@ -121,8 +120,8 @@ export default class Log implements LogOptions {
return;
}

this._writeToConsole(message, LogLevels.error);
return this._writeToStream(message, LogLevels.error);
this.writeToConsole(message, LogLevels.error);
return this.writeToStream(message, LogLevels.error);
}

/**
@@ -135,8 +134,8 @@ export default class Log implements LogOptions {
return;
}

this._writeToConsole(message, LogLevels.warn);
return this._writeToStream(message, LogLevels.warn);
this.writeToConsole(message, LogLevels.warn);
return this.writeToStream(message, LogLevels.warn);
}

/**
@@ -149,8 +148,8 @@ export default class Log implements LogOptions {
return;
}

this._writeToConsole(message, LogLevels.info);
return this._writeToStream(message, LogLevels.info);
this.writeToConsole(message, LogLevels.info);
return this.writeToStream(message, LogLevels.info);
}

/**
@@ -163,8 +162,8 @@ export default class Log implements LogOptions {
return;
}

this._writeToConsole(message, LogLevels.debug);
return this._writeToStream(message, LogLevels.debug);
this.writeToConsole(message, LogLevels.debug);
return this.writeToStream(message, LogLevels.debug);
}

/**
@@ -172,9 +171,9 @@ export default class Log implements LogOptions {
* @param {string} message `string` The message to write.
* @param {LogLevels} level `LogLevels` The level to write.
*/
private _writeToConsole(message: string, level: LogLevels): void {
protected writeToConsole(message: string, level: LogLevels): void {
if (this.console) {
console.log(this._formatMessage(message, level, true));
console.log(this.formatMessage(message, level, true));
}
}

@@ -184,8 +183,8 @@ export default class Log implements LogOptions {
* @param {LogLevels} level `LogLevels` The level to write.
* @returns {string} `string` The formatted message that was logged.
*/
private _writeToStream(message: string, level: LogLevels): string {
message = this._formatMessage(message, level) + '\n';
protected writeToStream(message: string, level: LogLevels): string {
message = this.formatMessage(message, level) + '\n';
this.stream.write(message);
return message;
}
@@ -197,13 +196,13 @@ export default class Log implements LogOptions {
* @param {boolean?} forConsole `boolean | undefined` Whether to add Chalk styling for the console.
* @returns {string} `string` The formatted message.
*/
private _formatMessage(
protected formatMessage(
message: string,
level: LogLevels,
forConsole?: boolean
): string {
const levelString: string = this._getFormattedLevel(level, forConsole);
const timestamp: string = this._getTimestamp(forConsole);
const levelString: string = this.getFormattedLevel(level, forConsole);
const timestamp: string = this.getTimestamp(forConsole);

message = String(message).replace(/\n/g, '␊');

@@ -220,28 +219,50 @@ export default class Log implements LogOptions {
* @param {boolean?} forConsole `boolean | undefined` Whether to add Chalk styling for the console.
* @returns {string} `string` The string representation of the wanted level.
*/
private _getFormattedLevel(wanted: LogLevels, forConsole?: boolean): string {
protected getFormattedLevel(wanted: LogLevels, forConsole?: boolean): string {
let level = '';

if (wanted === LogLevels.error) {
level = 'Error';
} else if (wanted === LogLevels.warn) {
level = 'Warn ';
} else if (wanted === LogLevels.info) {
level = 'Info ';
} else {
level = 'Debug';
switch (wanted) {
case LogLevels.error: {
level = 'Error';
break;
}

case LogLevels.warn: {
level = 'Warn ';
break;
}

case LogLevels.info: {
level = 'Info ';
break;
}

default: {
level = 'Debug';
}
}

if (forConsole === true) {
if (wanted === LogLevels.error) {
level = chalk.black.bgRed(level);
} else if (wanted === LogLevels.warn) {
level = chalk.black.bgYellow(level);
} else if (wanted === LogLevels.info) {
level = chalk.black.bgGreen(level);
} else {
level = chalk.black.bgCyan(level);
switch (wanted) {
case LogLevels.error: {
level = chalk.black.bgRed(level);
break;
}

case LogLevels.warn: {
level = chalk.black.bgYellow(level);
break;
}

case LogLevels.info: {
level = chalk.black.bgGreen(level);
break;
}

default: {
level = chalk.black.bgCyan(level);
}
}
}

@@ -253,7 +274,7 @@ export default class Log implements LogOptions {
* @param {boolean?} forConsole `boolean | undefined` Whether to add Chalk styling for the console.
* @returns {string} `string` The string timestamp, the string will be empty if the timestamp format is empty.
*/
private _getTimestamp(forConsole?: boolean): string {
protected getTimestamp(forConsole?: boolean): string {
if (this.timestamp.length === 0) {
return '';
}


+ 9
- 9
tests/index.test.ts View File

@@ -149,16 +149,16 @@ test('Log should output timestamp if defined', (t: Context): void => {
const startsWithNumbers = /^\d{4} /;

// Test the output and make sure it starts with the defined format.
const errorMessage: string = log.error('Error with timestamp.')!;
const errorMessage: string = log.error('Error with timestamp.') ?? '';
t.regex(errorMessage, startsWithNumbers);

const warnMessage: string = log.error('Warn with timestamp.')!;
const warnMessage: string = log.error('Warn with timestamp.') ?? '';
t.regex(warnMessage, startsWithNumbers);

const infoMessage: string = log.error('Info with timestamp.')!;
const infoMessage: string = log.error('Info with timestamp.') ?? '';
t.regex(infoMessage, startsWithNumbers);

const debugMessage: string = log.error('Debug with timestamp.')!;
const debugMessage: string = log.error('Debug with timestamp.') ?? '';
t.regex(debugMessage, startsWithNumbers);
});

@@ -176,16 +176,16 @@ test('Log should not output timestamp if disabled', (t: Context): void => {
const startsWithLetter = /^[a-z]/;

// Test the output and make sure it starts with the defined format.
const errorMessage: string = log.error('Error without timestamp.')!;
const errorMessage: string = log.error('Error without timestamp.') ?? '';
t.regex(errorMessage.toLowerCase(), startsWithLetter);

const warnMessage: string = log.error('Warn without timestamp.')!;
const warnMessage: string = log.error('Warn without timestamp.') ?? '';
t.regex(warnMessage.toLowerCase(), startsWithLetter);

const infoMessage: string = log.error('Info without timestamp.')!;
const infoMessage: string = log.error('Info without timestamp.') ?? '';
t.regex(infoMessage.toLowerCase(), startsWithLetter);

const debugMessage: string = log.error('Debug without timestamp.')!;
const debugMessage: string = log.error('Debug without timestamp.') ?? '';
t.regex(debugMessage.toLowerCase(), startsWithLetter);
});

@@ -230,7 +230,7 @@ test('Log should replace newlines with LF character', (t: Context): void => {

// Test if the newlines in the message get replaced with the ␊ character.
t.regex(
t.context.log.debug('Log\nwith\nnewlines.')!,
t.context.log.debug('Log\nwith\nnewlines.') ?? '',
/Log␊with␊newlines.\n$/
);
});


+ 52
- 46
tests/snapshots/index.test.ts.md View File

@@ -1,21 +1,24 @@
# Snapshot report for `test/index.test.ts`
# Snapshot report for `tests/index.test.ts`

The actual snapshot is saved in `index.test.ts.snap`.

Generated by [AVA](https://avajs.dev).

## Create log with non-default options
## Create log with only required options

> Snapshot 1

Log {
_filepath: 'logs/Testing.log',
console: true,
level: 1,
console: false,
level: 2,
name: 'Testing',
stream: Writable {
_events: {},
_eventsCount: 0,
stream: RotatingFileStream {
_events: {
close: Function {},
finish: Function {},
},
_eventsCount: 2,
_maxListeners: undefined,
_writableState: WritableState [
afterWriteTickInfo: null,
@@ -25,7 +28,9 @@ Generated by [AVA](https://avajs.dev).
bufferProcessing: false,
buffered: [],
bufferedIndex: 0,
closeEmitted: false,
closed: false,
constructed: true,
corked: 0,
decodeStrings: true,
defaultEncoding: 'utf8',
@@ -34,7 +39,7 @@ Generated by [AVA](https://avajs.dev).
ended: false,
ending: false,
errorEmitted: false,
errored: false,
errored: null,
finalCalled: false,
finished: false,
highWaterMark: 16384,
@@ -48,27 +53,51 @@ Generated by [AVA](https://avajs.dev).
writecb: null,
writelen: 0,
writing: false,
[Symbol(kOnFinished)]: [],
],
createGzip: Function value {},
exec: Function exec {},
filename: 'logs/Testing.log',
fsClose: Function close {},
fsCreateReadStream: Function createReadStream {},
fsCreateWriteStream: Function createWriteStream {},
fsMkdir: Function mkdir {},
fsOpen: Function open {},
fsReadFile: Function readFile {},
fsRename: Function rename {},
fsStat: Function stat {},
fsUnlink: Function unlink {},
fsWrite: Function write {},
fsWriteFile: Function writeFile {},
generator: Function {},
maxTimeout: 2147483640,
options: {
compress: 'gzip',
history: 'logs/Testing.log.txt',
interval: {
num: 5,
unit: 'd',
},
maxFiles: 6,
path: '',
},
[Symbol(kCapture)]: false,
},
timestamp: 'HH:mm:ss,SSS',
timestamp: 'YYYY-MM-DD HH:mm:ss,SSS',
}

## Create log with only required options
## Create log with non-default options

> Snapshot 1

Log {
_filepath: 'logs/Testing.log',
console: false,
level: 2,
console: true,
level: 1,
name: 'Testing',
stream: RotatingFileStream {
_events: {
close: Function {},
finish: Function {},
},
_eventsCount: 2,
stream: Writable {
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
_writableState: WritableState [
afterWriteTickInfo: null,
@@ -78,7 +107,9 @@ Generated by [AVA](https://avajs.dev).
bufferProcessing: false,
buffered: [],
bufferedIndex: 0,
closeEmitted: false,
closed: false,
constructed: true,
corked: 0,
decodeStrings: true,
defaultEncoding: 'utf8',
@@ -87,7 +118,7 @@ Generated by [AVA](https://avajs.dev).
ended: false,
ending: false,
errorEmitted: false,
errored: false,
errored: null,
finalCalled: false,
finished: false,
highWaterMark: 16384,
@@ -101,34 +132,9 @@ Generated by [AVA](https://avajs.dev).
writecb: null,
writelen: 0,
writing: false,
[Symbol(kOnFinished)]: [],
],
createGzip: Function value {},
exec: Function exec {},
filename: 'logs/Testing.log',
fsClose: Function close {},
fsCreateReadStream: Function createReadStream {},
fsCreateWriteStream: Function createWriteStream {},
fsMkdir: Function mkdir {},
fsOpen: Function open {},
fsReadFile: Function readFile {},
fsRename: Function rename {},
fsStat: Function stat {},
fsUnlink: Function unlink {},
fsWrite: Function write {},
fsWriteFile: Function writeFile {},
generator: Function {},
maxTimeout: 2147483640,
options: {
compress: 'gzip',
history: 'logs/Testing.log.txt',
interval: {
num: 5,
unit: 'd',
},
maxFiles: 6,
path: '',
},
[Symbol(kCapture)]: false,
},
timestamp: 'YYYY-MM-DD HH:mm:ss,SSS',
timestamp: 'HH:mm:ss,SSS',
}

BIN
tests/snapshots/index.test.ts.snap View File


+ 1622
- 2081
yarn.lock
File diff suppressed because it is too large
View File


Loading…
Cancel
Save