0.0.85 • Published 4 years ago

@grund/logging v0.0.85

Weekly downloads
318
License
MIT
Repository
-
Last release
4 years ago

@grund/logging

The package exposes the following things:

  • LoggingService
    • The class that contains pretty much all logic. More about this further down.
  • Logging
    • A singleton instance of the LoggingService.
  • trace, debug, info, warn, error, fatal
    • All logging functions available in @grund/logging.
    • Originates from Logging.

LoggingService

This class contains pretty much all logic in the package. It has a few different purposes:

  1. Store logging configuration - which controls some aspects of the logging actions.
  2. Instantiate and store the logging functions
  3. Expose some utility functions (e.g. identify and groupArguments)
  4. Extend the EventService class in order to enable listener and hook functionality. This enables us to listen/hook into every logging function that is fired. Read more about this in the @grund/events documentation.

Logging functions

The logging function are listed above. All of them takes in an arbitrary number of arguments and prints them with the console.log function - prefixed with a uppercase title depending on what logging level (trace, debug, etc) is used.

All logging function also exposes a silent property. This is a function that acts the exact same way as the original function, but doesn't print anything. In other words it only fires the listeners and hooks.

Internally, all log functions are wrappers around the same functions. These wrapping functions only purpose is to create a data interface called LogOptions which is then passed to the internal functions and guides their behaviors. This interface might be good to know exists and it looks like:

interface LogOptions {
	level: LOG_LEVEL;
	isSilent: boolean;
}

Usage

import { debug, warn, error } from '@grund/logging';

debug('Hello'); // Prints "[DEBUG] Hello"
warn.silent('Foo'); // Prints nothing but fires the listeners.
error('Yolo'); // Prints "[ERROR] Yolo"

Events

As mentioned above, LoggingService implements the EventService. The event schema is the following:

namespace LoggingEventSchema {
	interface Listeners {
		log: (options: LogOptions, loggingArgs: LoggingArgs) => any;

		trace: (...args: LoggingArgs) => any;
		debug: (...args: LoggingArgs) => any;
		info: (...args: LoggingArgs) => any;
		warn: (...args: LoggingArgs) => any;
		error: (...args: LoggingArgs) => any;
		fatal: (...args: LoggingArgs) => any;

		identify: (...args: IdentifyArgs) => any;
	}

	interface Hooks {
		beforeLog: (options: LogOptions, args: LoggingArgs) => any;
		beforeIdentify: (...args: IdentifyArgs) => any;
	}
}

Worth noting is that the log listener is always fired before any of the specific log function listeners.

Usage

import { Logging } from '@grund/logging';

Logging.listeners.on('debug', () => {
	console.log('Debug fired');
});
Logging.listeners.on('log', (options, loggingArgs) => {
	console.log(`Log fired! Level: ${options.log}`);
});
Logging.hooks.on('beforeLog', (options, loggingArgs) => {
	console.log('Before log');
});

debug('Hello');

// First prints "Before log"
// Prints "Log fired! Level: debug"
// Prints "Debug fired"

Identification

In order to implement some sort of production logging or error tracking one needs to be able to identify what user is generating the logs. For this we have the identify function which is exposed by LoggingService (and, in turn, the Logging singleton). The function itself doesn't do much, but acts more as a relay and common interface to other solutions.

For instance, this is used beautifully in collaboration with the @grund/logging-sentry. This package listens to whenever you call identify and forwards it to the Sentry library. In this way you don't need to interact with the Sentry library by yourself. This functionality can of course be extended to any third party logging package.

Usage

import { Logging } from '@grund/logging';

Logging.listeners.on('identify', (userId) => console.log({ userId }));

Logging.identify(1); // Prints "{ userId: 1 }" through the listener

Configuration

The LoggingService contains a "global" property called config that determines some of the logging behavior. The current configurations are:

  • Logging.config.print.isEnabled
    • Determines if the logs should be printed with console.log.
    • Defaults to IS_DEV.
  • Logging.config.print.isGrouped
    • If true, the arguments are grouped by the types string, Error and plain object.
    • Used by other libraries (e.g. @grund/logging-sentry).
    • Defaults to false.
  • Logging.errors.shouldObjectify
    • Used when grouping the arguments (by the function Logging.groupArguments)
    • If true and any error argument contains the function toObject, call it and use the return value instead of the error itself.
    • Defaults to true
  • Logging.errors.shouldIncludeStack
    • Determines if to include the stacks when objectifying the errors.
    • Defaults to true

Usage

import { debug, Logging } from '@grund/logging';

Logging.config.print.isEnabled = false;

debug('Hello'); // Prints nothing
0.0.84

4 years ago

0.0.85

4 years ago

0.0.80

4 years ago

0.0.81

4 years ago

0.0.82

4 years ago

0.0.83

4 years ago

0.0.78

4 years ago

0.0.79

4 years ago

0.0.76

4 years ago

0.0.77

4 years ago

0.0.73

4 years ago

0.0.75

4 years ago

0.0.70

4 years ago

0.0.71

4 years ago

0.0.72

4 years ago

0.0.68

4 years ago

0.0.66

4 years ago

0.0.67

4 years ago

0.0.65

4 years ago

0.0.64

4 years ago

0.0.63

4 years ago

0.0.62

4 years ago

0.0.61

4 years ago

0.0.60

4 years ago

0.0.59

4 years ago

0.0.58

4 years ago

0.0.54

5 years ago

0.0.53

5 years ago

0.0.52

5 years ago

0.0.51

5 years ago

0.0.50

5 years ago

0.0.49

5 years ago

0.0.48

5 years ago

0.0.46

5 years ago

0.0.47

5 years ago

0.0.45

5 years ago

0.0.43

5 years ago

0.0.44

5 years ago

0.0.42

5 years ago

0.0.41

5 years ago

0.0.40

5 years ago

0.0.38

5 years ago

0.0.39

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.30

5 years ago

0.0.31

5 years ago

0.0.32

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.24

5 years ago

0.0.22

5 years ago

0.0.23

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.10

5 years ago

0.0.11

5 years ago

0.0.12

5 years ago

0.0.13

5 years ago

0.0.14

5 years ago

0.0.3

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago