Giter Site home page Giter Site logo

rbx-log's Introduction

Not associated with BLAMMO ;-)

Structured logging library for Roblox, akin to (and inspired by) serilog. It uses the Message Templates spec for handling logging.

Setup

To begin, you will need to install the package. This can be done via

npm i @rbxts/log

Once installed, to begin logging you will need to configure the logger. (The server/client will need separate configurations, this is the recommended way of doing it)

Basic setup:

import Log, { Logger } from "@rbxts/log";
Log.SetLogger(
    Logger.configure()
        .WriteTo(Log.RobloxOutput()) // WriteTo takes a sink and writes to it
        .Create() // Creates the logger from the configuration
);

Log.Info("Hello, Log!");

The main power of this library comes from the structured event data logging:

const startPoint = new Vector2(0, 0)
const position = new Vector2(25, 134);
const distance = position.sub(startPoint).Magnitude;

Log.Info("Walked to {@Position}, travelling a distance of {Distance}", position, distance);

Log uses message templates, like serilog and will format strings with named parameters (positional coming soon).

The example above has two properties, Position and Distance, in the log event the @ operator in front of position tells Log to serialize the object passed in, rather than using tostring(value). The listed data types this library can serialize is listed below.

Rendered into JSON using HttpService, these properties appear alongside the Timestamp, Level and Template like:

{"Position": {"X": 25, "Y": 134}, "Distance": 136.32 }

The structured nature of the data means that it is easily searched and filtered by external tools (as well as roblox-based libraries like Zircon)

Of course, this data can be logged to the roblox console or another supported console directly if need be, the default Roblox Output sink for example displays the above as such:

08:29:20 [INF] Walked to {"X": 25, "Y": 134}, travelling a distance of 136.32

Features

  • Level-based logging, with levels like Debug, Information, Warning and Error.
  • Support for custom sinks, like logging to your own external server or to a console like the roblox output and Zircon.
  • The ability to enrich logging events using EnrichWithProperty or Enrich. E.g. add the version to your logging events:
    Log.SetLogger(
        Logger.configure()
            // ...
            .EnrichWithProperty("Version", PKG_VERSION) // Will add "Version" to the event data
            // ...
            .Create()
    );
  • A global Log object, with the ability to create individual Logger objects.

Supported Sinks

Sink Name Via Information
Roblox Output Log.RobloxOutput() Built in sink which will write to the output + dev console
Zircon Zircon.Log.Console() Runtime Debugging Console for Roblox

Use with Flamework

Flamework is a very useful dependency injection transformer for roblox-ts, in which we can use @rbxts/log quite extensively like you would with regular DI.

A simple approach to the DI logging is to just use ForContext - however, this is a bit more work and more explicit.

@Service()
export class MyService {
    public readonly logger = Log.ForContext(MyService);
}

Instead, we can use the dependency resolution feature of Flamework so that we can just refer to the Logger object from the constructor :-

(e.g. in index.server.ts & index.client.ts)

import Log, { Logger } from "@rbxts/log";
Modding.registerDependency<Logger>((ctor) => {
    return Log.ForContext(ctor); // will register this under the given DI class
});

Then in our above example:

@Service()
export class MyService {
    public constructor(private readonly logger: Logger) {}
}

rbx-log's People

Contributors

vorlias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

tanshaohwee

rbx-log's Issues

Cannot find index or typing

error node_modules/@rbxts/log/out/Core/TypeUtils.d.ts:1:26 - error TS2307: Cannot find module 'index' or its corresponding type declarations.
1 import { LogEvent } from "index";

Typedoc really doesn't like this incorrect typing. Hopefully can be resolved.

Render unknown Roblox datatypes in tables differently

Right now, if I log a table with, say, a CFrame inside it, and make it print to the Roblox output, it renders like so:

Log.Info("Some table: {Value}", { myKey: new CFrame() });
[INFO] Some table: {"myKey":null}

The table output is okay. However, myKey's value is interpreted as null, and this makes reading this output pretty confusing! Could Roblox data types be interpreted like the below?

{"myKey":<CFrame>}

or maybe even with the CFrame's components? (I doubt people would really be interested in this method though.)

{"myKey":CFrame(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)}

The same thing happens with stuff like Instances - they get printed as null.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.