Skip to content

Commit

Permalink
Cleanup. Docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 20, 2024
1 parent 218ea15 commit b5cda8b
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 90 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/bun.yml

This file was deleted.

32 changes: 0 additions & 32 deletions .github/workflows/deno.yml

This file was deleted.

12 changes: 2 additions & 10 deletions .github/workflows/jsr.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
name: Publish to jsr
name: Publish to jsr.io
on:
release:
types: [released]

workflow_dispatch:

jobs:
publish:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- uses: actions/checkout@v4

- name: Publish package
run: npx jsr publish
uses: cross-org/workflows/.github/workflows/jsr-publish.yml@main
22 changes: 0 additions & 22 deletions .github/workflows/node.yml

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Testing CI

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch: # Allow manual runs

jobs:
deno_ci:
uses: cross-org/workflows/.github/workflows/deno-ci.yml@main
with:
entrypoint: mod.ts
bun_ci:
uses: cross-org/workflows/.github/workflows/bun-ci.yml@main
with:
jsr_dependencies: "@cross/test @std/assert @cross/deepmerge @cross/utils"
node_ci:
uses: cross-org/workflows/.github/workflows/node-ci.yml@main
with:
jsr_dependencies: "@cross/test @std/assert @cross/deepmerge @cross/utils"
test_target: "*.test.ts"
4 changes: 4 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class Log {
*/
transports: LogTransport[] = [];

/**
* Constructs a new Log instansce
* @param transports - An array of transports, defaults to [new ConsoleLogger()]
*/
constructor(transports?: LogTransport[]) {
if (!transports) {
this.transports.push(new ConsoleLogger()); // Default
Expand Down
22 changes: 19 additions & 3 deletions transports/base.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { NumericSeverity, Severity } from "../src/types.ts";

/**
* Base configuration options for a LogTransport instance.
*/
export interface LogTransportBaseOptions {
/**
* Minimum severity to be logged, can be overridden with severities
* The minimum severity level for a message to be logged. If a message's severity
* is lower than this value, it will be ignored. This option is overridden if
* the `severities` option is provided.
*/
minimumSeverity?: Severity;

/**
* Takes precedence over minimumSeverity
* An array of specific severity levels to log. Messages with severity levels
* not included in this array will be ignored. This option takes precedence over
* `minimumSeverity`.
*/
severities?: Severity[];
}
Expand All @@ -33,10 +40,19 @@ export interface LogTransport {
}

/**
* Base class for Log Transports.
* Base class for Log Transports. Provides core logging functionality and
* configuration management. Extend this to build custom transports.
*/
export abstract class LogTransportBase implements LogTransport {

/** Options to be used, will be initialized with defaults */
protected options: LogTransportBaseOptions;

/**
* Default configuration options for LogTransportBase. Intended for typical
* logging scenarios where all messages with 'Info' severity or higher should
* be recorded.
*/
protected defaults: LogTransportBaseOptions;
constructor() {
this.defaults = {
Expand Down
22 changes: 20 additions & 2 deletions transports/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@ import { deepMerge } from "@cross/deepmerge";
import { LogTransportBase, LogTransportBaseOptions } from "./base.ts";
import { Severity } from "../src/types.ts";

interface ConsoleLoggerOptions extends LogTransportBaseOptions {
/**
* Configuration options for the ConsoleLogger transport. Extends the
* base logging options for all log transports.
*/
export interface ConsoleLoggerOptions extends LogTransportBaseOptions {
/**
* The minimum severity level for a message to be logged to the console.
* If a message's severity is lower than this value, it will not be displayed.
* This option is overridden if the `severities` option is provided.
*/
minimumSeverity?: Severity;

/**
* An array of specific severity levels to log to the console. Messages
* with severity levels not included in this array will be ignored. This
* option takes precedence over `minimumSeverity`.
*/
severities?: Severity[];
}

export class ConsoleLogger extends LogTransportBase {
options: ConsoleLoggerOptions;
/**
* Options for the ConsoleLogger transport
*/
public options: ConsoleLoggerOptions;

/**
* Constructs a ConsoleLogger instance.
Expand Down
9 changes: 8 additions & 1 deletion transports/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { Severity } from "../src/types.ts";
import { deepMerge } from "@cross/deepmerge";

interface FileLoggerOptions extends LogTransportBaseOptions {
export interface FileLoggerOptions extends LogTransportBaseOptions {
minimumSeverity?: Severity;
severities?: Severity[];

Expand All @@ -21,7 +21,13 @@ interface FileLoggerOptions extends LogTransportBaseOptions {
fileFormat?: "json" | "txt";
}

/**
* File Logger Transport. Supports logging to files using txt or json format.
*/
export class FileLogger extends LogTransportBase implements LogTransport {
/**
* Options for the file logger transport
*/
options: FileLoggerOptions;

/**
Expand All @@ -41,6 +47,7 @@ export class FileLogger extends LogTransportBase implements LogTransport {
options,
)!;
}

/**
* Logs a message to the configured file if the severity is at or above the transport's log level.
*
Expand Down
71 changes: 67 additions & 4 deletions transports/newrelic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,55 @@ import {
import { Severity } from "../src/types.ts";
import { deepMerge } from "@cross/deepmerge";

interface NewRelicLoggerOptions extends LogTransportBaseOptions {
apiKey: string; // The user's New Relic API key
region?: string; // The New Relic region (e.g., 'US', 'EU')
/**
* Configuration options for the New Relic logger. Extends the base logging options.
*/
export interface NewRelicLoggerOptions extends LogTransportBaseOptions {
/**
* Your New Relic Insights Insert API key. This is required for sending logs.
*/
apiKey: string;

/**
* The New Relic region where your data is stored.
* Valid values: 'US', 'EU', 'FedRamp'. Defaults to 'US'.
*/
region?: string;

/**
* An attribute name to identify the service generating the logs.
* Will be added to log events in New Relic.
*/
serviceAttribute?: string;

/**
* An attribute name to categorize the log type. Will be added to
* log events in New Relic.
*/
logtypeAttribute?: string;

/**
* An attribute name to specify the hostname of the machine generating the logs.
* Will be added to log events in New Relic.
*/
hostnameAttribute?: string;
}

/**
* A Log Transport implementation that sends log events to New Relic Insights.
*/
export class NewRelicLogger extends LogTransportBase implements LogTransport {

/**
* Options for the NewRelicLogger Transport
*/
options: NewRelicLoggerOptions;

/**
* Creates a new NewRelicLogger instance.
*
* @param options - Configuration options for the New Relic logger.
*/
constructor(options: NewRelicLoggerOptions) {
super();
this.options = deepMerge(
Expand All @@ -25,6 +63,14 @@ export class NewRelicLogger extends LogTransportBase implements LogTransport {
)!;
}

/**
* Logs a message to New Relic Insights. Used as an entrypoint for each log into this transport.
*
* @param severity - The severity level of the message.
* @param scope - An optional scope to categorize or group the log message.
* @param data - An array of data to be logged.
* @param timestamp - The timestamp of the log entry.
*/
log(severity: Severity, scope: string, data: unknown[], timestamp: Date) {
if (this.shouldLog(severity)) {
const serializedData = this.serializeToText(data);
Expand All @@ -38,6 +84,15 @@ export class NewRelicLogger extends LogTransportBase implements LogTransport {
}
}

/**
* Formats a log event in a structure suitable for New Relic Insights.
*
* @param severity - The severity level of the message.
* @param scope - The scope of the log message.
* @param data - The serialized log data.
* @param timestamp - The timestamp of the log entry.
* @returns A formatted event object ready to be sent to New Relic.
*/
private formatEvent(
severity: Severity,
scope: string,
Expand All @@ -55,6 +110,11 @@ export class NewRelicLogger extends LogTransportBase implements LogTransport {
};
}

/**
* Sends a log event to New Relic Insights. Handles potential errors.
*
* @param event - The formatted log event object.
*/
private async sendToNewRelic(event: object) {
const endpoint = this.getEndpoint();
try {
Expand All @@ -77,7 +137,10 @@ export class NewRelicLogger extends LogTransportBase implements LogTransport {
console.error("Network error sending log event to New Relic:", error);
}
}

/**
* Determines the correct New Relic Insights API endpoint based on the configured region.
* @returns The API endpoint URL as a string.
*/
private getEndpoint(): string {
if (this.options.region === "US") {
return "https://log-api.newrelic.com/log/v1";
Expand Down
Loading

0 comments on commit b5cda8b

Please sign in to comment.