Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
uspasojevic96 committed Dec 24, 2020
0 parents commit dc81dc1
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './logger';
export * from './log-level';
5 changes: 5 additions & 0 deletions src/log-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum LogLevel {
WARNING = 1,
ERROR = 2,
INFO = 0
}
50 changes: 50 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
},
}

0 comments on commit dc81dc1

Please sign in to comment.