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

@gallolabs/docker-logs-watcher

v0.1.2

Published

Javascript Docker logs watcher

Downloads

72

Readme

Follow docker logs based on criteria :)

  • [x] Follow on container patterns (using component matcher, on container name, id, image, compose, labels), stdout/stdin/both
  • [x] Realtime
  • [x] Docker disconnects tolerance
  • [x] Very fast containers (unless others tested components, but this is thanks to a small hack)
  • [ ] Possibly non-realtime logs in case of long disconnections. In case of disconnect, it reconnects requesting logs since last log to fetch missed logs. If the disconnection was some seconds, it makes sense (depending of the realtime window). Why not define a max "realtime" gap/window ?
  • [ ] Optimize container stream using dual stdout/stderr when both are watched ?
  • [ ] Unordered logs in some cases (very fast loggin in stdout and stderr). Example : docker run node:16-alpine sh -c 'echo OK; echo ERROR >&2; exit 1' will show in random order the messages, also in the console. Adding -t option resolves, but impact the container. Probably no fix, even with attach api.
  • [ ] Using run -t outputs only in stdout. The order is respected. Note that in the console also it is to STDOUT. Probably no fix.
  • [ ] Multiline support
  • [X] watch as stream
  • [ ] Subscrive containers with dedicated stream(s)
  • [ ] Add events to replace logger

Motivations

The main goal of the tool is to read in realtime the logs of my containers and makes some metrics (errors, operations) and see them in grafana with alerts.

THIS IS NOT a tool to collect logs. I tested some tools like logspout, interesting because it can be used to collect logs AND to consume them, but the projects seems to be not maintened. Using a tool as container to collect logs or configuring the logging driver (thanks to dual-logging, you also can read log with docker daemon) like Loki is more appropriated (but it was disastrous for me).

How to use

import { DockerLogs } from '@gallolabs/docker-logs-watcher'

const abortController = new AbortController

const dockerLogs = new DockerLogs()

dockerLogs.watch({
    containerMatches: { name: ['*', '!*special*'] },
    stream: 'both',
    onLog(log) {
        const name = log.container.compose
            ? log.container.compose.project + '/' + log.container.compose.service
            : log.container.name
        console.log(log.stream.toUpperCase(), log.date.toISOString(), '-', name, '-', log.message)
    },
    abortSignal: abortController.signal
})

dockerLogs.watch({
    containerMatches: { compose: { project: 'special' } },
    stream: 'stderr',
    onLog(log) {
        console.log('SPECIAL STDERR', log.date.toISOString(), '-', log.container.name, '-', log.message)
    },
    abortSignal: abortController.signal
})

const stream = dockerLogs.stream({
    containerMatches: { name: '*special*' },
    stream: 'both',
    abortSignal: abortController.signal
})

stream.on('data', console.log)
stream.pipe(otherStreamObjectMode)

for await (const log of dockerLogs.stream({
    stream: 'both',
    abortSignal: abortController.signal
})) {
    console.log(log)
}

setTimeout(() => { abortController.abort() }, 30000)

will produce with my tests (a docker compose with a a micro script that says start, then work then crashes with badadoom to test last log on crash (bug with Docker in some versions)) :

STDERR 2023-07-07T23:49:05.916366512Z - docker-logs/test - Error: Badaboom
STDOUT 2023-07-07T23:49:06.542877623Z - docker-logs/test - start
SPECIAL STDERR 2023-07-07T23:49:14.062961523Z - very-special-container - ERRORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
STDOUT 2023-07-07T23:49:16.555776232Z - docker-logs/test - work
STDERR 2023-07-07T23:49:26.568193389Z - docker-logs/test - Error: Badaboom
STDOUT 2023-07-07T23:49:27.331012301Z - docker-logs/test - start