LoginSignup
0

More than 5 years have passed since last update.

bashで色付きlogger

Posted at

ただ色つけてTLSVで出力するだけ

test.sh
#!/bin/bash

batch_logger(){
  message=${1:?}
  LOGLEVEL=${2:-INFO}
  case ${3:-NORMAL} in
    'RED' )   COLOR_HEADER='\e[1;31m' ;COLOR_FOOTER='\e[m' ;;
    'BLUE')   COLOR_HEADER='\e[1;34m' ;COLOR_FOOTER='\e[m' ;;
    'YELLOW') COLOR_HEADER='\e[1;33m' ;COLOR_FOOTER='\e[m' ;;
    'GREEN')  COLOR_HEADER='\e[1;32m' ;COLOR_FOOTER='\e[m' ;;
    * )       COLOR_HEADER='' ;COLOR_FOOTER='' ;;
  esac
  /bin/echo -e "time:$(date +'%Y/%m/%d:%H:%M:%S')\tclass:$(basename $0)\thost:$(hostname)\tLEVEL:${LOGLEVEL}\tmessage:${COLOR_HEADER}${message}${COLOR_FOOTER}" 
}

batch_logger "普通のメッセージ"
batch_logger "警告のメッセージ" WARN YELLOW
batch_logger "エラーメッセージ" ERROR RED
batch_logger "正常終了メッセージ" INFO GREEN

test.png

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0