Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syslog - How to Show Colorized Messages?

I need to clarify that I am not looking to colorize the log output, and I am only interested in the program outputs that are written to syslog.

So here is the scenario, I have a systemd unit service that runs a script, which indicates the 256 colors in Bash.

Here is the Service Unit File:

[Unit]
Description=Bash Color Service

[Service]
Type=simple
EnvironmentFile=/etc/environment
ExecStart=/home/username/colors.sh

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=bashcolors

Restart=on-failure
RestartSec=10

User=username
Group=username

[Install]
WantedBy=multi-user.target

And this is the Bash script under /home/username/colors.sh (grabbed it from here):

#!/bin/bash

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.

for fgbg in 38 48 ; do # Foreground / Background
    for color in {0..255} ; do # Colors
        # Display the color
        printf "\e[${fgbg};5;%sm  %3s  \e[0m" $color $color
        # Display 6 colors per lines
        if [ $((($color + 1) % 6)) == 4 ] ; then
            echo # New line
        fi
    done
    echo # New line
done

exit 0

Running this script gives out some colored output to my terminal, like this:

enter image description here

Alright, now to the actual problem. When I run the service, the syslog output does include the escape color characters, but are not displayed in color. Here is a snippet output of tail -f /var/log/syslog with relevant information:

bashcolor[19892]: #033[48;5;202m  202  #033[0m#033[48;5;203m  203  #033[0m#033[48;5;204m  204  #033[0m#033[48;5;205m  205  #033[0m#033[48;5;206m  206  #033[0m#033[48;5;207m  207  #033[0m
bashcolor[19892]: #033[48;5;208m  208  #033[0m#033[48;5;209m  209  #033[0m#033[48;5;210m  210  #033[0m#033[48;5;211m  211  #033[0m#033[48;5;212m  212  #033[0m#033[48;5;213m  213  #033[0m
bashcolor[19892]: #033[48;5;214m  214  #033[0m#033[48;5;215m  215  #033[0m#033[48;5;216m  216  #033[0m#033[48;5;217m  217  #033[0m#033[48;5;218m  218  #033[0m#033[48;5;219m  219  #033[0m
bashcolor[19892]: #033[48;5;220m  220  #033[0m#033[48;5;221m  221  #033[0m#033[48;5;222m  222  #033[0m#033[48;5;223m  223  #033[0m#033[48;5;224m  224  #033[0m#033[48;5;225m  225  #033[0m
bashcolor[19892]: #033[48;5;226m  226  #033[0m#033[48;5;227m  227  #033[0m#033[48;5;228m  228  #033[0m#033[48;5;229m  229  #033[0m#033[48;5;230m  230  #033[0m#033[48;5;231m  231  #033[0m
bashcolor[19892]: #033[48;5;232m  232  #033[0m#033[48;5;233m  233  #033[0m#033[48;5;234m  234  #033[0m#033[48;5;235m  235  #033[0m#033[48;5;236m  236  #033[0m#033[48;5;237m  237  #033[0m
bashcolor[19892]: #033[48;5;238m  238  #033[0m#033[48;5;239m  239  #033[0m#033[48;5;240m  240  #033[0m#033[48;5;241m  241  #033[0m#033[48;5;242m  242  #033[0m#033[48;5;243m  243  #033[0m
bashcolor[19892]: #033[48;5;244m  244  #033[0m#033[48;5;245m  245  #033[0m#033[48;5;246m  246  #033[0m#033[48;5;247m  247  #033[0m#033[48;5;248m  248  #033[0m#033[48;5;249m  249  #033[0m
bashcolor[19892]: #033[48;5;250m  250  #033[0m#033[48;5;251m  251  #033[0m#033[48;5;252m  252  #033[0m#033[48;5;253m  253  #033[0m#033[48;5;254m  254  #033[0m#033[48;5;255m  255  #033[0m

Things I Have Tried So Far:

  • using the ccze utility: tail -f /var/log/syslog | ccze -A
  • echo -ne $(tail -f /var/log/syslog)
  • using multitail, grc and rainbow utilities instead of tail
like image 800
user3707763 Avatar asked Oct 17 '18 19:10

user3707763


1 Answers

syslog escapes the ansi color codes by default [1]. To enable add the following to the /etc/rsyslog.conf

$EscapeControlCharactersOnReceive off

Then restart rsyslog and your service.

$ systemctl restart rsyslog
$ systemctl restart my_color_service
like image 117
iamauser Avatar answered Nov 15 '22 04:11

iamauser