generated from nhcarrigan/template
### Explanation _No response_ ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning Major - My pull request introduces a breaking change. Reviewed-on: #1 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import type { Level } from "./types/level.js";
|
||||
|
||||
/**
|
||||
* This is a wrapper around our custom alert monitoring server. This class
|
||||
* allows you to pipe errors and log messages to our server.
|
||||
*/
|
||||
export class Logger {
|
||||
private readonly url: string;
|
||||
|
||||
/**
|
||||
* Instantiates the class.
|
||||
* @param application -- The name of the application (this will appear in logs).
|
||||
* @param token -- Your API token for the monitoring service.
|
||||
* @param url -- (Optional) The URL for your own alerting instance.
|
||||
*/
|
||||
public constructor(
|
||||
private readonly application: string,
|
||||
private readonly token: string,
|
||||
url?: string,
|
||||
) {
|
||||
this.url = url ?? "https://alerts.nhcarrigan.com";
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a log message to the alerting service.
|
||||
* @param level -- The level of the log message.
|
||||
* @param message -- The message to send.
|
||||
*/
|
||||
public async log(level: Level, message: string): Promise<void> {
|
||||
await fetch(`${this.url}/log`, {
|
||||
body: JSON.stringify({
|
||||
application: this.application,
|
||||
level: level,
|
||||
message: message,
|
||||
}),
|
||||
headers: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Standard header name.
|
||||
"Authorization": this.token,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Standard header name.
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an error to the alerting service.
|
||||
* @param context -- A brief description of where the error occurred (E.G. Function name).
|
||||
* @param error -- The Node.js error object.
|
||||
*/
|
||||
public async error(context: string, error: Error): Promise<void> {
|
||||
await fetch(`${this.url}/error`, {
|
||||
body: JSON.stringify({
|
||||
application: this.application,
|
||||
context: context,
|
||||
message: error.message,
|
||||
stack: error.stack ?? "No stack trace available.",
|
||||
}),
|
||||
headers: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Standard header name.
|
||||
"Authorization": this.token,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Standard header name.
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
/**
|
||||
* Defines the levels we allow for logging. This is honestly
|
||||
* entirely arbitrary, but ensures some level of consistency across
|
||||
* our production applications.
|
||||
*/
|
||||
export type Level = "debug" | "info" | "warn";
|
||||
Reference in New Issue
Block a user