Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 18, 2024
1 parent cc4e0d9 commit 4a40b89
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 81 deletions.
52 changes: 37 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
**@cross/log - Generic logger for Node.js, Deno, and Bun**

**Installation**
## Installation

Instructions are available at
[https://jsr.io/@cross/log](https://jsr.io/@cross/log). Here's the quick
Expand All @@ -17,9 +17,7 @@ npx jsr add @cross/log
bunx jsr add @cross/log
```

**Getting Started**

**Simplest Use Case**
## Getting Started

```javascript
import { Log } from "@cross/log";
Expand All @@ -44,30 +42,54 @@ logger.info("Hello log!");
- **Global and Transport Filtering:** Set log levels globally or configure
fine-grained filtering per transport.

**Core Classes**

- **Log:** Central class for managing log transports and dispatching log
messages.
- `debug(message: string)`
- `info(message: string)`
- `warn(message: string)`
- `error(message: string)`
### Example Usage

**Example Usage (with Custom Transports)**
#### Console + File Transport

```javascript
import { ConsoleLogger, FileLogger, Log, Severity } from "./mod.ts";

const myLogger = new Log([
new ConsoleLogger({
// Only write severity Info and higher to console
minimumSeverity: Severity.Info
minimumSeverity: Severity.Info,
// Also possible to select individual severities
// severities: [Severity.Info, Severity.Error]
}),
new FileLogger({
filePath: "./app.log",
fileFormat: "txt"
fileFormat: "txt",
}),
]);

myLogger.debug("Initializing application...");
myLogger.warn("Received a potentially invalid request");
try {
// ... code that might fail
} catch (error) {
myLogger.error("Critical failure!", error);
}
```

#### Splunk HEC Transport

1. **Obtain Splunk HEC endpoint and token:**
- Log into your Splunk instance.
- Navigate to **Settings > Data Inputs > HTTP Event Collector**.
- Create a new input or locate an existing one, noting its endpoint URL and
token.

2. **Integrate the Splunk HEC Transport:**

```javascript
import { ConsoleLogger, Log, Severity, SplunkHecClient } from "@cross/log";

const myLogger = new Log([
new ConsoleLogger({ minimumSeverity: Severity.Info }),
new SplunkHecClient({
hecEndpoint: "https://splunk-server:8088/services/collector",
hecToken: "your-splunk-hec-token",
sourceType: "my_app_logs", // Adjust as needed
}),
]);

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/log",
"version": "0.10.0",
"version": "0.10.1",
"exports": {
".": "./mod.ts",
"./console": "./transports/console.ts",
Expand Down
73 changes: 36 additions & 37 deletions src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Severity, NumericSeverity } from "./types.ts";
import { NumericSeverity, Severity } from "./types.ts";

export interface LogTransportBaseOptions {
/**
Expand All @@ -10,7 +10,7 @@ export interface LogTransportBaseOptions {
* Takes precedence over minimumSeverity
*/
severities?: Severity[];
};
}

/**
* Interface for defining the core functionality of a Log Transport.
Expand All @@ -36,40 +36,39 @@ export interface LogTransport {
* Base class for Log Transports.
*/
export abstract class LogTransportBase implements LogTransport {
protected options: LogTransportBaseOptions;
protected defaults: LogTransportBaseOptions;
constructor() {
this.defaults = {
minimumSeverity: Severity.Info,
};
this.options = this.defaults;
}

/**
* Abstract method for logging events. To be implemented by specific transports
*/
abstract log(
severity: Severity,
scope: string,
data: unknown[],
timestamp: Date,
): void;

/**
* Determines if the message should be logged based on its severity and the configured log level.
* @param level - The severity level of the message.
* @returns True if the message should be logged, false otherwise.
*/
protected shouldLog(severity: Severity): boolean {
// Check severities list first (if present)
if (this.options.severities) {
return this.options.severities.includes(severity);
} else {
// Fallback to minimum severity check
const minimumLevel = this.options.minimumSeverity ?? Severity.Debug;
return NumericSeverity.get(severity)! >=
NumericSeverity.get(minimumLevel)!;
}
protected options: LogTransportBaseOptions;
protected defaults: LogTransportBaseOptions;
constructor() {
this.defaults = {
minimumSeverity: Severity.Info,
};
this.options = this.defaults;
}

/**
* Abstract method for logging events. To be implemented by specific transports
*/
abstract log(
severity: Severity,
scope: string,
data: unknown[],
timestamp: Date,
): void;

/**
* Determines if the message should be logged based on its severity and the configured log level.
* @param level - The severity level of the message.
* @returns True if the message should be logged, false otherwise.
*/
protected shouldLog(severity: Severity): boolean {
// Check severities list first (if present)
if (this.options.severities) {
return this.options.severities.includes(severity);
} else {
// Fallback to minimum severity check
const minimumLevel = this.options.minimumSeverity ?? Severity.Debug;
return NumericSeverity.get(severity)! >=
NumericSeverity.get(minimumLevel)!;
}
}
}
28 changes: 13 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@

/**
* Map for convenient comparison of log levels by numeric value.
*/
export const NumericSeverity: Map<string, number> = new Map([
["DEBUG", 100],
["INFO", 200],
["LOG", 300],
["WARN", 400],
["ERROR", 500],
]);


["DEBUG", 100],
["INFO", 200],
["LOG", 300],
["WARN", 400],
["ERROR", 500],
]);

/**
* Enumeration of available log levels.
*/
export enum Severity {
Debug = "DEBUG",
Info = "INFO",
Log = "LOG",
Warn = "WARN",
Error = "ERROR",
}
Debug = "DEBUG",
Info = "INFO",
Log = "LOG",
Warn = "WARN",
Error = "ERROR",
}
9 changes: 2 additions & 7 deletions transports/console.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
// deno-lint-ignore-file
import { Colors } from "@cross/utils";
import { deepMerge } from "@cross/deepmerge";
import {
LogTransportBase,
LogTransportBaseOptions,
} from "../src/transport.ts";
import {
Severity,
} from "../src/types.ts";
import { LogTransportBase, LogTransportBaseOptions } from "../src/transport.ts";
import { Severity } from "../src/types.ts";

interface ConsoleLoggerOptions extends LogTransportBaseOptions {
minimumSeverity?: Severity;
Expand Down
4 changes: 1 addition & 3 deletions transports/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
LogTransportBase,
LogTransportBaseOptions,
} from "../src/transport.ts";
import {
Severity,
} from "../src/types.ts";
import { Severity } from "../src/types.ts";

import { deepMerge } from "@cross/deepmerge";

Expand Down
4 changes: 1 addition & 3 deletions transports/splunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {
LogTransportBase,
LogTransportBaseOptions,
} from "../src/transport.ts";
import {
Severity,
} from "../src/types.ts";
import { Severity } from "../src/types.ts";
import { deepMerge } from "@cross/deepmerge";

interface SplunkHecClientOptions extends LogTransportBaseOptions {
Expand Down

0 comments on commit 4a40b89

Please sign in to comment.