7

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
user3707763
  • 121
  • 1
  • 8

2 Answers2

14

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
iamauser
  • 11,119
  • 5
  • 34
  • 52
1

Old question, but for completeness, if you have already logged something with colors, and syslog has escaped the escape characters, you can colorize the recorded log in your terminal by piping the logs to sed:

tail /var/log/syslog | sed 's/#033/\x1b/g'

Explanation:

All colors changes are defined with control sequences which start with hexadecimal \x1b (the escape character). Syslog has escaped the escape character and turned them to #033 because \033 is the that same character in octal system. You may reverse that action by changing any #033 into \x1b.

Demo enter image description here


Here's also a nice intro to the topic: https://chrisyeh96.github.io/2020/03/28/terminal-colors.html

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96