Aller au contenu principal

Log-Right

Making the world a better place... one log at a time

Simplified logging with plug and play event processing

Error Handling

Log-Right's `Result` class provides a standard way to handle returning errors (or any other value) from a function or external process. By using the Result object as your go-to method of returning from a function, you are organizing all of your code's function types, keeping to a consistent and predicable error handling principle, and enabling your code to use the rest of the Log-Right event handling flow.

Formatting

Log-Right will first format Result objects using a method provided by the user (or the default methods if not provided), so that the data is in a form that can be captured to the logs. Log-Right also has various build-in methods of formatting this data that the user can select from.

Logging

Logging in JavaScript/TypeScript can unfortunately tend to be much more difficult than just writing a string to the console. Log-Right has plenty of built in functionality to support various methods of capturing this data to the source of your choice - but if you just want to write a string to the console, Log-Right has you covered there too!

The Result Handler Flow

Log-Right introduces the Result object which can be returned from any function or process to indicate if it was successful (and if not, what failed). The verification function can then be called to do the following:

  1. Check if the Result should be logged/reported (based on it's severity).
  2. Format the Result data into something that can be provided to the logs.
  3. Capture the formatted Result data to the configured logging method.
  4. Call a user defined callback function.
  5. Check if the Result is considered an Error (based on it's severity).
/src/utils/greaterThanFive.ts

import { Result } from "log-result";

export type GreaterThanFiveReturn = {
a: string;
b: boolean;
}

export const greaterThanFive(param: number): Result<GreaterThanFiveReturn> {
try {
if (param =< 5) {
return Result.error("param is not greater than 5");
}

const returnValues: GreaterThanFiveReturn = {
a: "some string",
b: false
}

return Result.success(returnValues);
} catch (e) {
return Result.error(e);
}
}

Using Log-Right Return Types

Log-Right aims to standardize returning values and/or errors from functions. The success and error static functions in the Result class can be used to quickly generate a Result object instance. The success function also accepts return values as a parameter that can be used in the calling function.

Handling Result Objects

In the calling function, it is very simple to handle the Result object that was passed up from the returning function. The Result class has a method `validate` that will return true if the returning function succeeded and false if it failed. The validate function will also trigger the rest of the Result handler flow (formatting and capturing the logs, invoking callbacks, etc.).

info
The validate function will only start the calls to the the Result handler flow the first time it is called for that object. This is to prevent duplicate logging events and callbacks.
/src/app/numberProcessing.ts

import { Result } from "log-result";
import { greaterThanFive, GreaterThanFiveReturn } from "../../utils/greaterThanFive.ts";

export const numberProcessing(param: number): Result<GreaterThanFiveReturn> {
try {
const result: Result<GreaterThanFiveReturn> = greaterThanFive(param);
if (!result.validate()) {
// greaterThanFive failed to execute
return result;
}

// greaterThanFive was successful
const returnValues: GreaterThanFiveReturn = result.data;

// returnValues = { a: "some string", b: false }

return Result.success(returnValues);
} catch (e) {
return Result.error(e);
}
}

Simple

The library's APIs are extremely easy to use and learn. Plus, everything you need to know is included in our detailed documentation.

Modular

Aside from the core functionality that comes with the log-right package, additional functionality can be added through our growing list of plugin packages.

TypeScript Support

Log-Right is created using TypeScript, so all the types associated with the package are fully supported, available for use, and up to date.

Performance

The async-process package calls all event handling and logging modules in a separate web worker thread, so that main thread is free to continue processing without the additional overhead.

Light-Weight

Package modules are separated by functionality/utility, so the imports are very light-weight and work great with webpack, Browserify, or Rollup and also supports tree-shaking!

Configuration

Log-Right can be configuration as little or as much as the user desires. All handlers, events, and logs are customizable; however, we defined various default configurations sets, so this is not required.

Are you ready to get started with Log-Right?

Author

Patrick Sullivan
A person with a passion for learning new things.

Finger Print: 1BD2 7192 7770 2549 F4C9 F238 E6AD C420 DA5C 4C2D