Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
joseywoermann committed Nov 20, 2021
0 parents commit bda326a
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Josey Wörmann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Yet another logging module

## Install

```sh
npm i @josey/logger
```

## Examples

### Basic setup

```ts
import Logger from "@josey/logger";

const logger = new Logger("UTC");

logger.info("This is an example!");
```

### Advanced setup with custom colors & explicit types

```ts
import Logger from "@josey/logger";

const logger = new Logger("local", {
colors: {
DEBUG: "#9fa6a4",
INFO: "#32a852",
WARN: "#edb611",
ERROR: "#ff0000",
},
});

logger.debug<string>("Hello", "there!");
logger.info<number>(1, 2, 3, 4, 5, 6, 7, 8, 9);
logger.warn<boolean>(false, true);
logger.error<string[]>(["a", "b", "c"]);
```

### Available methods

This module provides the following methods, to all which you can pass any number of arguments of any type.

```ts
Logger.debug<T>(...messages: T[])
Logger.info<T>(...messages: T[])
Logger.warn<T>(...messages: T[])
Logger.error<T>(...messages: T[])
```

### Configuration

it is possible to modify the loggers behaviour and appereance in the constructor.

The fist argument determines whether to use UTC or local time.
The second argument can be used to change the colors used to highlight the log level.

```ts
import Logger from "@josey/logger";

// Either "UTC" or "local"
const logger = new Logger("local", {
// Pass 4 hex-color string to change the color palette
colors: {
DEBUG: "#9fa6a4",
INFO: "#32a852",
WARN: "#edb611",
ERROR: "#ff0000",
},
});
```
146 changes: 146 additions & 0 deletions package-lock.json

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

32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@josey/logger",
"version": "1.0.0",
"description": "Yet another logging module",
"type": "module",
"main": "./dist/src/index.js",
"scripts": {
"build": "tsc"
},
"author": "Josey Wörmann <jcw05@gmx.de>",
"license": "MIT",
"repository": {
"url": "https://github.com/joseywoermann/logger"
},
"bugs": {
"url": "https://github.com/joseywoermann/logger/issues"
},
"dependencies": {
"chalk": "^4.1.2"
},
"devDependencies": {
"typescript": "^4.5.2"
},
"types": "./dist/src/index.d.ts",
"exports": {
"import": "./dist/src/index.js"
},
"files": [
"dist",
"src"
]
}
110 changes: 110 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// @ts-expect-error
import chalk from "chalk";

type Timezone = "UTC" | "local";

interface Config {
colors: Colors;
}

interface Colors {
DEBUG: string;
INFO: string;
WARN: string;
ERROR: string;
}

enum DefaultColors {
DEBUG = "#9DD1BA",
INFO = "#BAD755",
WARN = "#FDD662",
ERROR = "#FD647A",
}

/**
* Yet another logging module
* @author Josey Wörmann <jcw05@gmx.de>
* @see https://github.com/joseywoermann/logger
*/
export default class Logger {
readonly #timezone: Timezone;
readonly #config: Config = {
colors: DefaultColors,
};

/**
* Create a new Logger instance with a given time zone setting.
* @param timezone Either `"UTC"` or `"local"`. This decides whether to use the local timezone or UTC as the time.
* @param config Additional settings like custom colors.
*/
constructor(timezone: Timezone, config?: Config) {
this.#timezone = timezone;
this.#config = config ?? this.#config;
}

/**
* Logs a debug message to the console.
* @param messages
*/
public debug = <T>(...messages: T[]): void => {
console.debug(`${this.time()} ${chalk.hex(this.#config.colors.DEBUG)("[DEBUG]")}`, ...messages);
};

/**
* Logs an info message to the console.
* @param messages
*/
public info = <T>(...messages: T[]): void => {
console.info(`${this.time()} ${chalk.hex(this.#config.colors.INFO)("[INFO] ")}`, ...messages);
};

/**
* Logs a warning to the console.
* @param messages
*/
public warn = <T>(...messages: T[]): void => {
console.warn(`${this.time()} ${chalk.hex(this.#config.colors.WARN)("[WARN] ")}`, ...messages);
};

/**
* Logs an error to the console.
* @param messages
*/
public error = <T>(...messages: T[]): void => {
console.error(`${this.time()} ${chalk.hex(this.#config.colors.ERROR)("[ERROR]")}`, ...messages);
};

/**
* An internal helper method that returns a formatted string with time and date according to the user's time zone preference.
* @returns
*/
private time = (): string => {
const now = new Date();

if (this.#timezone === "UTC") {
return `[${now.getUTCFullYear()}-${this.format(now.getUTCMonth() + 1)}-${this.format(
now.getUTCDate()
)}] [${this.format(now.getUTCHours())}:${this.format(now.getUTCMinutes())}:${this.format(
now.getUTCSeconds()
)}]`;
} else {
return `[${now.getFullYear()}-${this.format(now.getMonth() + 1)}-${this.format(
now.getDate()
)}] [${this.format(now.getHours())}:${this.format(now.getMinutes())}:${this.format(
now.getSeconds()
)}]`;
}
};

/**
* We want our values to always be double-digit (except for year), this internal helper function guarantees that.
* @param date
* @returns
*/
private format = (date: string | number): string => {
if (typeof date === "number") {
date = date.toString();
}
return date.length === 1 ? `0${date}` : date;
};
}
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"outDir": "dist",
"moduleResolution": "Node",
"target": "ESNext",
"declaration": true
}
}

0 comments on commit bda326a

Please sign in to comment.