@ricdotnet/logger 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ import { Logger } from '../src/Logger';
2
+ import { Constants } from '../src/Constants';
3
+
4
+ const loggerSpy = {
5
+ info : jest.spyOn(Logger, 'info'),
6
+ warn : jest.spyOn(Logger, 'warn'),
7
+ error: jest.spyOn(Logger, 'error')
8
+ };
9
+
10
+ const consoleSpy = {
11
+ info : jest.spyOn(console, 'log'),
12
+ warn : jest.spyOn(console, 'warn'),
13
+ error: jest.spyOn(console, 'error'),
14
+ };
15
+
16
+ describe('Logger test', () => {
17
+
18
+ beforeEach(() => {
19
+ loggerSpy.info.mockClear();
20
+ loggerSpy.warn.mockClear();
21
+ loggerSpy.error.mockClear();
22
+ });
23
+
24
+ it('can log an info and check the output', () => {
25
+ Logger.info('hello world');
26
+
27
+ expect(loggerSpy.info).toHaveBeenCalledWith('hello world');
28
+ expect(consoleSpy.info).toHaveBeenCalledWith(`${Constants.TEXT_BLUE}[INFO]: hello world`);
29
+ });
30
+
31
+ it('can log a warning', () => {
32
+ Logger.warn('hello world');
33
+
34
+ expect(loggerSpy.warn).toHaveBeenCalledWith('hello world');
35
+ expect(consoleSpy.warn).toHaveBeenCalledWith(`${Constants.TEXT_YELLOW}[WARN]: hello world`);
36
+ });
37
+
38
+ it('can log an error', () => {
39
+ Logger.error('hello world');
40
+
41
+ expect(loggerSpy.error).toHaveBeenCalledWith('hello world');
42
+ expect(consoleSpy.error).toHaveBeenCalledWith(`${Constants.TEXT_RED}[ERROR]: hello world`);
43
+ });
44
+ });
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { Logger } from './src/Logger';
package/jest.config.js ADDED
@@ -0,0 +1,11 @@
1
+ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ roots: [
6
+ '<rootDir>',
7
+ ],
8
+ moduleDirectories: [
9
+ 'node_modules'
10
+ ],
11
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@ricdotnet/logger",
3
+ "version": "0.0.1",
4
+ "description": "A logger utility for my personal projects.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "start": "node dist/index.js",
8
+ "dev": "ts-node index.ts",
9
+ "build": "rm -rf ./dist && tsc",
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [],
13
+ "author": "ricdotnet <me@rrocha.uk> https://ricr.dev",
14
+ "repository": "",
15
+ "license": "ISC",
16
+ "devDependencies": {
17
+ "@types/jest": "^27.4.1",
18
+ "jest": "^27.5.1",
19
+ "ts-jest": "^27.1.4",
20
+ "ts-node": "^10.7.0",
21
+ "typescript": "^4.6.3"
22
+ }
23
+ }
File without changes
@@ -0,0 +1,27 @@
1
+ export enum Constants {
2
+ Reset = '\x1b[0m',
3
+ BRIGHT = '\x1b[1m',
4
+ Dim = '\x1b[2m',
5
+ Underscore = '\x1b[4m',
6
+ Blink = '\x1b[5m',
7
+ Reverse = '\x1b[7m',
8
+ Hidden = '\x1b[8m',
9
+
10
+ TEXT_BLACK = '\x1b[30m',
11
+ TEXT_RED = '\x1b[31m',
12
+ TEXT_GREEN = '\x1b[32m',
13
+ TEXT_YELLOW = '\x1b[33m',
14
+ TEXT_BLUE = '\x1b[34m',
15
+ TEXT_MAGENTA = '\x1b[35m',
16
+ TEXT_CYAN = '\x1b[36m',
17
+ TEXT_WHITE = '\x1b[37m',
18
+
19
+ BG_BLACK = '\x1b[40m',
20
+ BG_RED = '\x1b[41m',
21
+ BG_GREEN = '\x1b[42m',
22
+ BG_YELLOW = '\x1b[43m',
23
+ BG_BLUE = '\x1b[44m',
24
+ BG_MAGENTA = '\x1b[45m',
25
+ BG_CYAN = '\x1b[46m',
26
+ BG_WHITE = '\x1b[47m',
27
+ }
package/src/Logger.ts ADDED
@@ -0,0 +1,65 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as path from 'path';
3
+ import { Constants } from './Constants';
4
+
5
+ // some places use any just because I do not want to deal with multiple types of data...
6
+ // I guess generics would be cool here but eh
7
+ // TODO: Multiple log folders for [info, warn, error]
8
+
9
+ export class Logger {
10
+
11
+ static LOG_DIR = './Logs';
12
+
13
+ static async checkLogDir() {
14
+ const dirs = await fs.readdir('./');
15
+ if ( !dirs.includes(this.LOG_DIR.split('/')[1]) ) {
16
+ await fs.mkdir(this.LOG_DIR);
17
+ return this.info('Logs folder generated.');
18
+ }
19
+
20
+ // return this.info('Logs folder already exists... The logger will write inside it.');
21
+ }
22
+
23
+ static async info(message: any) {
24
+ await this.writeToFile(message, 'info');
25
+ console.log(`${Constants.TEXT_BLUE}[INFO]: ${message}`);
26
+ }
27
+
28
+ static async warn(message: any) {
29
+ await this.writeToFile(message, 'warn');
30
+ console.warn(`${Constants.TEXT_YELLOW}[WARN]: ${message}`);
31
+ }
32
+
33
+ static async error(message: any) {
34
+ await this.writeToFile(message, 'error');
35
+ console.error(`${Constants.TEXT_RED}[ERROR]: ${message}`);
36
+ }
37
+
38
+ static async writeToFile(message: any, type: string) {
39
+ const DAY: number = new Date().getDate();
40
+ const MONTH: number = new Date().getMonth() + 1; // +1 because it goes 0-11
41
+ const YEAR: number = new Date().getFullYear();
42
+ const HOURS: number = new Date().getHours();
43
+ const MINUTES: number = new Date().getMinutes();
44
+
45
+ const FULL_DATE: string = [DAY, MONTH, YEAR].join('-');
46
+ const FULL_TIME: string = [HOURS, MINUTES].join(':');
47
+
48
+ let logFile;
49
+ try {
50
+ logFile = await fs.readFile(path.join(this.LOG_DIR, `${FULL_DATE}-${type}.log`));
51
+ } catch (e) {
52
+ // console.error(e);
53
+ // let me just fail silently if the file does not exist
54
+ }
55
+
56
+ if ( !logFile ) {
57
+ const data = 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
58
+ return await fs.writeFile(path.join(this.LOG_DIR, `${FULL_DATE}-${type}.log`), data);
59
+ }
60
+
61
+ const data = logFile + 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
62
+ return await fs.writeFile(path.join(this.LOG_DIR, `${FULL_DATE}-${type}.log`), data);
63
+ }
64
+
65
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "esnext",
5
+ "sourceMap": true,
6
+ "removeComments": true,
7
+ "outDir": "dist",
8
+ "sourceRoot": "./",
9
+ "allowJs": true,
10
+ "declaration": true
11
+ },
12
+ "exclude": [
13
+ "node_modules"
14
+ ]
15
+ }