Skip to content

Simple logging utility to pipe output lines into separate processes

License

Notifications You must be signed in to change notification settings

Bleskocvok/logwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

112 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logwrap

test and build badge coverage badge

Simple logging utility to pipe output lines into separate processes.

It can be useful in instances when you want to handle the output of a program in a separate way from the actual executable. E.g., you are unable to modify the program or the handling of logging is in a different language.

One of the main features is the option to coalesce consecutive lines to a single executed process using the command line option -s seconds.

Usage:

logwrap ‹app› ‹cmd_for_stdout› ‹cmd_for_stderr›
logwrap ‹app› [args...] -- ‹cmd_for_stdout› [args...] -- ‹cmd_for_stderr› [args...]
  • app is the command/executable to run
  • cmd_for_stdout is the command/executable to pipe stdout to
  • cmd_for_stderr is the command/executable to pipe stderr to

Show help with logwrap -h.

Example

Sample invocation

logwrap ./app -- ./send-email out -- ./send-email err

Suppose app writes these lines to standard output and then terminates. send-email is a supposed executable that is placed in the working folder and let's say, for the purpose of this example, will send an e-mail to a particular address.

A
B
Hello World

The ./send-email out command would be run three times, with each line passed to stdin. logwrap would wait for each invocation to complete before moving on to the next one.

In the case that the ./send-email executable takes a long time to perform its task, it might be useful to invoke logwrap with the -s option. This requires a number of seconds. When ./app produces a line of output, ./logwrap will wait the given number of seconds before executing the corresponding logging process (for stdout or stderr) and it will group any lines to the same instance of executed ./send-email.

In the above example, if ./logwrap was given the option -s 1.5 and lines A, B and Hello World were sent in the span of one second, after an additional 0.5 seconds, ./send-email out would be run once and all three lines will be passed to its standard input.

Another useful option is -d, which will achieve that each output process is run detached from the main process and ./app will never have to be delayed to wait for the completion of ./send-email. This will, however, introduce a race condition between the processes, which can result in a later line being logged before an earlier one.

So, for this specific use case, the utility could be run with these parameters.

logwrap -s 1.5 -d ./app -- ./send-email out -- ./send-email err

Another use case

If your application writes to a log file in /path/to/log and you want to fire alerts for particular types of messages, you can use it like this.

logwrap -- tail -f /path/to/log -- fire_alert

There fire_alert could be a simple script that checks if the log contains a line that should trigger an alert and then sends the alert.

Installation

You can download the latest static build on the release page.

Build

  1. Build

    These commands will clone the project and build the logwrap executable using make and c99. If you have a POSIX compatible system, the make and c99 utilities should be already present, so no need to have installed anything other than git to build this project.

    git clone https://github.com/Bleskocvok/logwrap.git
    cd logwrap
    make
    
  2. Install

    The resulting executable can be moved to a directory listed in PATH. Which can be for instance ~/bin.

    cp logwrap ~/bin/
    
  3. Run

    In any directory type to see if installed properly.

    logwrap -h
    

Todo

  • Implement option -l lines to work similarly to -s, but coalesce by a number of lines instead of amount of time
  • Provide a static executable release
  • 100% coverage
  • Option to prepend time before every line in given format

About

Simple logging utility to pipe output lines into separate processes

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published