From dc81dc15000d619158a8377a6d35a6c54cd9e4b5 Mon Sep 17 00:00:00 2001 From: Uros Spasojevic Date: Thu, 24 Dec 2020 21:57:17 +0100 Subject: [PATCH] Initial commit --- .gitignore | 34 ++++++++++++++++++++++++++++++++ package-lock.json | 19 ++++++++++++++++++ package.json | 31 +++++++++++++++++++++++++++++ src/index.ts | 2 ++ src/log-level.ts | 5 +++++ src/logger.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 14 +++++++++++++ 7 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 src/log-level.ts create mode 100644 src/logger.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ddaa61e --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# compiled output +/dist +/node_modules + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# OS +.DS_Store + +# Tests +/coverage +/.nyc_output + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace +*.iml +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..460f363 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,19 @@ +{ + "name": "@uspasojevic/logger", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "kleur": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.3.tgz", + "integrity": "sha512-H1tr8QP2PxFTNwAFM74Mui2b6ovcY9FoxJefgrwxY+OCJcq01k5nvhf4M/KnizzrJvLRap5STUy7dgDV35iUBw==" + }, + "typescript": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", + "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..43ff76f --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "@uspasojevic/logger", + "version": "1.0.0", + "description": "Basic logger", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/uspasojevic96/logger.git" + }, + "keywords": [ + "logger", + "logging" + ], + "author": "Uros Spasojevic", + "license": "ISC", + "bugs": { + "url": "https://github.com/uspasojevic96/logger/issues" + }, + "homepage": "https://github.com/uspasojevic96/logger#readme", + "devDependencies": { + "typescript": "^4.1.3" + }, + "dependencies": { + "kleur": "^4.1.3" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..76f20d7 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,2 @@ +export * from './logger'; +export * from './log-level'; diff --git a/src/log-level.ts b/src/log-level.ts new file mode 100644 index 0000000..921b808 --- /dev/null +++ b/src/log-level.ts @@ -0,0 +1,5 @@ +export enum LogLevel { + WARNING = 1, + ERROR = 2, + INFO = 0 +} \ No newline at end of file diff --git a/src/logger.ts b/src/logger.ts new file mode 100644 index 0000000..b2a4390 --- /dev/null +++ b/src/logger.ts @@ -0,0 +1,50 @@ +import {LogLevel} from "./log-level"; +import {cyan, red, white, yellow} from "kleur"; + +export class Logger { + private logLevel: LogLevel = LogLevel.ERROR; + + public log(level: LogLevel, tag: string, message: string): void { + switch(level) { + case LogLevel.ERROR: + this.logError(tag, message); + break; + case LogLevel.INFO: + this.logInfo(tag, message); + break; + case LogLevel.WARNING: + this.logWarning(tag, message); + break; + default: + break; + } + } + + private generatePrefix(tag: string): string { + let date: any = new Date().toISOString().split('T'); + date = `${date[0]} ${date[1].substring(0, date[1].length - 5)}`; + + return `${cyan(tag)} : ${date} : `; + } + + private logError(tag: string, message: string): void { + if (this.logLevel >= LogLevel.ERROR) { + const str = `${this.generatePrefix(tag)}${red('[ERROR]')} ${message}`; + console.log(str); + } + } + + private logInfo(tag: string, message: string): void { + if (this.logLevel >= LogLevel.INFO) { + const str = `${this.generatePrefix(tag)}${white('[INFO]')} ${message}`; + console.log(str); + } + } + + private logWarning(tag: string, message: string): void { + if (this.logLevel >= LogLevel.WARNING) { + const str = `${this.generatePrefix(tag)}${yellow('[WARNING]')} ${message}`; + console.log(str); + } + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..67aa7e5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + "strict": true, /* Enable all strict type-checking options. */ + "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + "skipLibCheck": true, /* Skip type checking of declaration files. */ + "rootDir": "src", + "outDir": "dist", + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, + "sourceMap": true, + "declaration": true + }, +}