Skip to content

Commit

Permalink
πŸŽ‰ Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
OfficialCRUGG committed Sep 30, 2022
1 parent 327c9e3 commit 4740e72
Show file tree
Hide file tree
Showing 17 changed files with 542 additions and 52 deletions.
63 changes: 35 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,79 @@
<div align="center">

# @blazingworks/helloworld
# @blazingworks/logger

![Lines of code](https://img.shields.io/tokei/lines/github/blazingworks/helloworld?style=for-the-badge)
![npm Downloads](https://img.shields.io/npm/dy/@blazingworks/helloworld?style=for-the-badge)
![GitHub issues](https://img.shields.io/github/issues/blazingworks/helloworld?style=for-the-badge)
![GitHub pull requests](https://img.shields.io/github/issues-pr/blazingworks/helloworld?style=for-the-badge)
![GitHub](https://img.shields.io/github/license/blazingworks/helloworld?style=for-the-badge)
![GitHub Repo stars](https://img.shields.io/github/stars/blazingworks/helloworld?style=for-the-badge)
![npm Version](https://img.shields.io/npm/v/@blazingworks/helloworld?style=for-the-badge)
![GitHub contributors](https://img.shields.io/github/contributors/blazingworks/helloworld?style=for-the-badge)
![Lines of code](https://img.shields.io/tokei/lines/github/blazingworks/logger?style=for-the-badge)
![npm Downloads](https://img.shields.io/npm/dy/@blazingworks/logger?style=for-the-badge)
![GitHub issues](https://img.shields.io/github/issues/blazingworks/logger?style=for-the-badge)
![GitHub pull requests](https://img.shields.io/github/issues-pr/blazingworks/logger?style=for-the-badge)
![GitHub](https://img.shields.io/github/license/blazingworks/logger?style=for-the-badge)
![GitHub Repo stars](https://img.shields.io/github/stars/blazingworks/logger?style=for-the-badge)
![npm Version](https://img.shields.io/npm/v/@blazingworks/logger?style=for-the-badge)
![GitHub contributors](https://img.shields.io/github/contributors/blazingworks/logger?style=for-the-badge)

</div>

🌍 Hello World NPM Library by BlazingWorks, acts as a boilerplate for future libraries

## Tech Stack

- [TypeScript](https://www.typescriptlang.org/)
- [Jest](https://jestjs.io/)
- [ESLint](https://eslint.org/)
- [Prettier](https://prettier.io/)

- [TypeScript](https://www.typescriptlang.org/)
- [Jest](https://jestjs.io/)
- [ESLint](https://eslint.org/)
- [Prettier](https://prettier.io/)

## Installation

### NPM

```bash
npm install @blazingworks/helloworld
npm install @blazingworks/logger
```

### Yarn

```bash
yarn add @blazingworks/helloworld
yarn add @blazingworks/logger
```

## Usage

### TypeScript

```typescript
import { helloWorld } from '@blazingworks/helloworld';
import { Logger, ConsoleTransport } from "@blazingworks/logger";

const logger = new Logger({
transports: [{ module: new ConsoleTransport() }],
});

console.log(helloWorld());
// or
console.log(helloWorld("Nagi Aoe"));
logger.info("Hello World");
```

### JavaScript

```javascript
const { helloWorld } = require('@blazingworks/helloworld');
const { Logger, ConsoleTransport } = require("@blazingworks/logger");

const logger = new Logger({
transports: [{ module: new ConsoleTransport() }],
});

console.log(helloWorld());
// or
console.log(helloWorld("Reiji Kurose"));
logger.info("Hello World");
```

### Transports

The logger comes with various kinds of transports. You can find more about them [here](https://github.com/blazingworks/logger/blob/main/TRANSPORTS.md).

## How to report issues/questions

- For general issues/questions, [open an issue](https://github.com/blazingworks/helloworld/issues)
- For security issues, please email [[email protected]](mailto:[email protected])
- For important questions, please email [[email protected]](mailto:[email protected])
- For general issues/questions, [open an issue](https://github.com/blazingworks/logger/issues)
- For security issues, please email [[email protected]](mailto:[email protected])
- For important questions, please email [[email protected]](mailto:[email protected])

## License

As this is an open-source project, support is limited. Please use [GitHub Issues](https://github.com/blazingworks/helloworld/issues) for community support or contact [[email protected]](mailto:[email protected]) for very important matters.
As this is an open-source project, support is limited. Please use [GitHub Issues](https://github.com/blazingworks/logger/issues) for community support or contact [[email protected]](mailto:[email protected]) for very important matters.

**ℹ️ All code in this repository is licensed under the [MIT License](LICENSE.md).**
103 changes: 103 additions & 0 deletions TRANSPORTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Transports

## Official Transports

- [ConsoleTransport](#ConsoleTransport)
- [RawTransport](#RawTransport)
- [CustomTransport](#CustomTransport)
- [FileTransport](#FileTransport)
- [PrettyConsoleTransport](#PrettyConsoleTransport)

### ConsoleTransport

The `ConsoleTransport` is a transport that logs to the console. It is included in the package.

#### Usage

```typescript
// ...
const logger = new Logger({
transports: [{ module: new ConsoleTransport("[%l] %m") }],
});
// ...
```

#### Options

| Name | Type | Default | Description |
| :----- | :----- | :-------- | :----------------------------- |
| format | string | "[%l] %m" | The format of the log message. |

### RawTransport

The `RawTransport` is a transport that logs to the console in JSON format without any customization. It is included in the package.

#### Usage

```typescript
// ...
const logger = new Logger({
transports: [{ module: new RawTransport() }],
});
// ...
```

### CustomTransport

The `CustomTransport` is a transport that logs to a custom function. It is included in the package.

#### Usage

```typescript
// ...
const logger = new Logger({
transports: [
{
module: new CustomTransport((level, message) => {
// ...
}),
},
],
});
```

### FileTransport

The `FileTransport` will be transport that logs to a file. It is currently not yet available and will be included in the package.

### PrettyConsoleTransport

The `PrettyConsoleTransport` will be transport that logs to the console in a pretty format. It is currently not yet available and will be made available through an external package.

#### Options

| Name | Type | Default | Description |
| :----- | :------- | :------ | :------------------------------------------------------------------------ |
| format | function | | The function to log the message. It takes in a message string and a level |

## Community Transports

There are no community transports right now. Feel free to add yours through a PR! :)

## How to add a transport

To add a transport, you need to create a class that implements the [`TransportModule`](https://github.com/blazingworks/logger/blob/main/src/interfaces/TransportModule.ts) interface.

Below, you can find an example transport based on the `ConsoleTransport` for reference:

```typescript
import { LogLevel } from "@blazingworks/logger/enums/LogLevel";
import TransportModule from "@blazingworks/logger/interfaces/TransportModule";

export default class MyCoolConsoleTransport implements TransportModule {
private format: string;

constructor(format = "(%l): %m") {
this.format = format;
}

handle(message: string, level: LogLevel): void {
console.log(this.format.replace("%l", level).replace("%m", message));
}
}
```
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@blazingworks/helloworld",
"name": "@blazingworks/logger",
"version": "1.0.0",
"description": "Hello World NPM Library by BlazingWorks, acts as a boilerplate for future libraries",
"main": "dist/helloworld.js",
"types": "dist/helloworld.d.ts",
"description": "TypeScript Logging Library developed by BlazingWorks",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"repository": "https://github.com/blazingworks/helloworld.git",
"repository": "https://github.com/blazingworks/logger.git",
"author": "BlazingWorks",
"license": "MIT",
"private": false,
Expand All @@ -29,5 +29,6 @@
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
}
},
"dependencies": {}
}
46 changes: 46 additions & 0 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { allLogLevels, LogLevel } from "./enums/LogLevel";
import LoggerOptions from "./interfaces/LoggerOptions";
import Transport from "./interfaces/Transport";

export default class Logger {
private transports: Transport[] = [];

constructor(options: LoggerOptions) {
options.transports.forEach((transport) => {
if (!transport.levels) transport.levels = allLogLevels();
this.transports.push(transport);
});
}

log(message: string, level: LogLevel = LogLevel.Info) {
this.transports.forEach((transport) => {
if (transport.levels?.includes(level)) {
transport.module.handle(message, level);
}
});
}

info(message: string) {
this.log(message, LogLevel.Info);
}

warn(message: string) {
this.log(message, LogLevel.Warn);
}

error(message: string) {
this.log(message, LogLevel.Error);
}

fatal(message: string) {
this.log(message, LogLevel.Fatal);
}

debug(message: string) {
this.log(message, LogLevel.Debug);
}

access(message: string) {
this.log(message, LogLevel.Access);
}
}
23 changes: 23 additions & 0 deletions src/enums/LogLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export enum LogLevel {
Debug = "debug",
Info = "info",
Warn = "warn",
Error = "error",
Fatal = "fatal",
Access = "access",
}

export function isLogLevel(level: string): level is LogLevel {
return Object.values(LogLevel).includes(level as LogLevel);
}

export function getLogLevel(level: string): LogLevel {
if (isLogLevel(level)) {
return level;
}
return LogLevel.Info;
}

export function allLogLevels(): LogLevel[] {
return Object.values(LogLevel);
}
7 changes: 0 additions & 7 deletions src/helloworld.ts

This file was deleted.

Empty file added src/index.ts
Empty file.
6 changes: 6 additions & 0 deletions src/interfaces/LoggerOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Transport from "./Transport";

export default interface LoggerOptions {
transports: Transport[];
messageFunction?: (message: string) => string;
}
6 changes: 6 additions & 0 deletions src/interfaces/Transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { LogLevel } from "../enums/LogLevel";
import TransportModule from "./TransportModule";
export default interface Transport {
module: TransportModule;
levels?: LogLevel[];
}
5 changes: 5 additions & 0 deletions src/interfaces/TransportModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LogLevel } from "../enums/LogLevel";

export default interface TransportModule {
handle(message: string, level: LogLevel): void;
}
13 changes: 13 additions & 0 deletions src/transports/ConsoleTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LogLevel } from "../enums/LogLevel";
import TransportModule from "../interfaces/TransportModule";
export default class ConsoleTransport implements TransportModule {
private format: string;

constructor(format = "[%l] %m") {
this.format = format;
}

handle(message: string, level: LogLevel): void {
console.log(this.format.replace("%l", level).replace("%m", message));
}
}
13 changes: 13 additions & 0 deletions src/transports/CustomTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LogLevel } from "../enums/LogLevel";
import TransportModule from "../interfaces/TransportModule";
export default class CustomTransport implements TransportModule {
private handler: (message: string, level: LogLevel) => void;

constructor(handler: (message: string, level: LogLevel) => void) {
this.handler = handler;
}

handle(message: string, level: LogLevel): void {
this.handler(message, level);
}
}
7 changes: 7 additions & 0 deletions src/transports/RawTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LogLevel } from "../enums/LogLevel";
import TransportModule from "../interfaces/TransportModule";
export default class RawTransport implements TransportModule {
handle(message: string, level: LogLevel): void {
console.log({ message, level });
}
}
Loading

0 comments on commit 4740e72

Please sign in to comment.