npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sysdlogger

v1.1.0

Published

Create and retrieve log entries from the systemd journal

Downloads

9

Readme

node-systemd-journal

Interact with linux systemd journal, adding entries and retrieving logs from NodeJS.

Only works on Linux

Usage

$ npm install --save sysdlogger

The in your code:

import { SysDLogger } from 'sysdlogger';

// instatiate with a tag
const syslogger = new SysDLogger({ tag: 'FOOBAR' });

(async() => {
    // writes with the provided tag
    await syslogger.write('This is a foobar entry for journal');
    // reads from journal by tag
    const res = await syslogger.read();
    console.log(res);
})();

You can check the added entry to journal like:

$ journalctl -t FOOBAR

Options This could be provided when instantiate or override when calling write or read.

const options = {
    // string; the choosen tag, if not provided the default will be 'SysDLogger'
    tag: 'SysDLogger', 
    // boolean; true will retrieve a JSON object with the log contenc
    json: false,
    // number of lines to retrieve from journal
    lines: 0,
    // if true will retrieve first latest entries
    reverse: false,
    // string; the choosen level or priority for logging, default: notice
    level: undefined, 
}

With options for writing and reading you can do things like:

import { SysDLogger } from 'sysdlogger';
import assert from 'node:assert';

(async() => {
    const myTag = 'NODE_SYSTEMD_JOURNAL';
    const message = 'an ugly pigeon and a fluffy squirrel';
    await syslogger.write(message, { tag: myTag });
    const slogJSON = await syslogger.read({
            tag: myTag,
            json: true,
            reverse: true,
            lines: 1,
            level: 'warn',
    });
    assert(slogJSON[0].SYSLOG_IDENTIFIER === myTag);
    assert(slogJSON[0].MESSAGE === message);
    assert(slogJSON[0].PRIORITY === 4);
    assert(typeof slogJSON === 'object');
})();

The properties of the JSON object from journal are:

[
  '_TRANSPORT',
  '_SYSTEMD_CGROUP',
  '_UID',
  '_SYSTEMD_USER_SLICE',
  'PRIORITY',
  '_AUDIT_LOGINUID',
  'SYSLOG_IDENTIFIER',
  '__MONOTONIC_TIMESTAMP',
  '_SYSTEMD_SLICE',
  '_SYSTEMD_UNIT',
  'SYSLOG_FACILITY',
  '_CAP_EFFECTIVE',
  '_SOURCE_REALTIME_TIMESTAMP',
  '_RUNTIME_SCOPE',
  '_MACHINE_ID',
  '__SEQNUM_ID',
  '_SYSTEMD_INVOCATION_ID',
  '_HOSTNAME',
  '__CURSOR',
  '_GID',
  '_SYSTEMD_OWNER_UID',
  '__REALTIME_TIMESTAMP',
  '_BOOT_ID',
  'MESSAGE',
  'SYSLOG_TIMESTAMP',
  '_AUDIT_SESSION',
  '_PID',
  '_SYSTEMD_USER_UNIT',
  '__SEQNUM',
  '_COMM'
]

The list of levels and the equivalent of PRIORITY JSON field:

[ 
    emerg: 0,
    alert: 1,
    crit: 2,
    err: 3,
    error: 3,
    warning: 4,
    warn: 4,
    notice: 5,
    info: 6,
    debug: 7
]

Also possible to use an alias to write a log message with a level like this:

const MESSAGE = 'a pigeon over the roof';

//add an entry to systemd-journal with debug level
await syslogger.debug(MESSAGE);

//add an entry to systemd-journal with info level
await syslogger.info(MESSAGE);

//add an entry to systemd-journal with notice level
await syslogger.notice(MESSAGE);

//add an entry to systemd-journal with warning level
await syslogger.warn(MESSAGE);

//add an entry to systemd-journal with error level
await syslogger.error(MESSAGE);

//add an entry to systemd-journal with critical level
await syslogger.crit(MESSAGE);

//add an entry to systemd-journal with alert level
await syslogger.alert(MESSAGE);

//add an entry to systemd-journal with emergency level
await syslogger.emerg(MESSAGE);

More info about Journal JSON format

More info also:

$ man journalctl

$ man logger


Feedback from usage and contributions are very welcome. Also if you like it, please leave a :star: I would appreciate it ;)