generated from blazingworks/helloworld
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
327c9e3
commit 4740e72
Showing
17 changed files
with
542 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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).** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
Oops, something went wrong.