log-helper 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash -e
  2. # log helper base on environment variable CONTAINER_LOG_LEVEL
  3. # CONTAINER_LOG_LEVEL environment variable is set by run tool based on --log-level argument (info by default)
  4. # or you can set it directly with docker --env argument
  5. # Usage example: log-helper info CONTAINER_LOG_LEVEL is info or more
  6. # the message "CONTAINER_LOG_LEVEL is info or more" will be printed only if log level is info, debug or trace
  7. LOG_LEVEL_NONE=0
  8. LOG_LEVEL_ERROR=1
  9. LOG_LEVEL_WARNING=2
  10. LOG_LEVEL_INFO=3
  11. LOG_LEVEL_DEBUG=4
  12. LOG_LEVEL_TRACE=5
  13. # default log level if CONTAINER_LOG_LEVEL is not set -> info
  14. log_level=${CONTAINER_LOG_LEVEL:-${LOG_LEVEL_INFO}}
  15. call=$1 # function to call (error, warning, info, debug, trace, level)
  16. if [[ ! "$call" =~ ^(error|warning|info|debug|trace|level)$ ]]; then
  17. echo "Error: Function $call not found"
  18. echo "Allowed functions are: error, warning, info, debug, trace, level"
  19. echo "usage example: log-helper info hello !"
  20. exit 1
  21. fi
  22. echo_msg="" # message to print if required log level is set
  23. echo_param="" # echo command parameters
  24. function print_log(){
  25. local level_txt=$1
  26. local message=$2
  27. local date=$(date +"%Y-%m-%d %T")
  28. readarray -t messages <<<"$message"
  29. for m in "${messages[@]}"; do
  30. echo "*** ${level_txt} | ${date} | ${m}"
  31. done
  32. }
  33. function error() {
  34. # getEchoParams no matter what level it is to not break pipes
  35. getEchoParams $@
  36. if [ $log_level -ge 1 ]; then
  37. echo $echo_param "$(print_log " ERROR " "$echo_msg")"
  38. fi
  39. }
  40. function warning() {
  41. # getEchoParams no matter what level it is to not break pipes
  42. getEchoParams $@
  43. if [ $log_level -ge 2 ]; then
  44. echo $echo_param "$(print_log "WARNING" "$echo_msg")"
  45. fi
  46. }
  47. function info() {
  48. # getEchoParams no matter what level it is to not break pipes
  49. getEchoParams $@
  50. if [ $log_level -ge 3 ]; then
  51. echo $echo_param "$(print_log " INFO " "$echo_msg")"
  52. fi
  53. }
  54. function debug() {
  55. # getEchoParams no matter what level it is to not break pipes
  56. getEchoParams $@
  57. if [ $log_level -ge 4 ]; then
  58. echo $echo_param "$(print_log " DEBUG " "$echo_msg")"
  59. fi
  60. }
  61. function trace() {
  62. # getEchoParams no matter what level it is to not break pipes
  63. getEchoParams $@
  64. if [ $log_level -ge 5 ]; then
  65. echo $echo_param "$(print_log " TRACE " "$echo_msg")"
  66. fi
  67. }
  68. function getMsgFromStdin() {
  69. if [ -z "$2" ]; then
  70. echo_msg=$(cat)
  71. fi
  72. }
  73. function getEchoParams() {
  74. echo_msg="$@"
  75. if [[ "$1" =~ ^(-e|-n|-E)$ ]]; then
  76. echo_param=$1
  77. echo_msg=${echo_msg#$1 }
  78. fi
  79. # read from pipe if echo_msg is empty
  80. [[ -n "$echo_msg" ]] || getMsgFromStdin
  81. }
  82. function level() {
  83. local operator=$1
  84. local loglevel_str=$2
  85. local loglevel_str=${loglevel_str^^} # uppercase
  86. if [[ ! "$operator" =~ ^(eq|ne|gt|ge|lt|le)$ ]]; then
  87. echo "Error: Operator $operator not allowed"
  88. echo "Allowed operators are: eq, ne, gt, ge, lt, le"
  89. echo "Help: http://www.tldp.org/LDP/abs/html/comparison-ops.html"
  90. exit 1
  91. fi
  92. if [ -z "$loglevel_str" ]; then
  93. echo "Error: No log level provided"
  94. echo "Allowed log level are: none, error, warning, info, debug, trace"
  95. echo "usage example: log-helper level eq info"
  96. exit 1
  97. fi
  98. local log_level_var=LOG_LEVEL_$loglevel_str
  99. if [ $log_level -$operator ${!log_level_var} ]; then
  100. exit 0
  101. else
  102. exit 1
  103. fi
  104. }
  105. shift
  106. $call "$@"