Our bash scripts use color for log-messages, but only, when stderr is a tty:
if [ -t 2 ]
then
function log {
# Use blue color for our messages, when stderr is a terminal
printf "%(%F %T)T %s e[1;34m%se[1;0mn" -1 ${0##*/} "$*" >&2
}
else
function log {
printf "%(%F %T)T %s %sn" -1 ${0##*/} "$*" >&2
}
fi
We’d like for the logs produced by the Java-programs to also be colored — but only, when written to a tty. I tried adding the %highlight{}
to the pattern of our existing appender:
<Appenders>
<Console name="stderr" target="SYSTEM_ERR">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} %highlight{%-5p} %c{1}:%L - %m%n"/>
</Console>
</Appenders>
but that turns coloring on unconditionally, even when stderr is redirected to a file — or into a pager.
Can it be told to automatically detect, whether the output is a TTY?