linux shell logging functions

Brothers in operation and maintenance, do you have any logos in the logs you output, and do the logos change in color? ? ? When I open the log file, there are large pieces of text, and my head is dizzy. It is difficult to find out which ones are error messages and which ones are normal output messages. What should I do? This is why everyone saw this article today, let's take a look at the specific implementation.

#!/bin/bash

function log() {
    level=$1
    content=$2
    curtime=$(date "+[%Y-%m-%d %H:%M:%S]")

    #  31m==红色;    32m==绿色;     33m==黄色;      34m==蓝色;
    case "$level" in
    debug)
        echo -e "${curtime}\033[32m[${level}]\033[0m ${content}"
        ;;
    info)
        echo -e "${curtime}\033[32m[${level}]\033[0m ${content}"
        ;;
    warning)
        echo -e "${curtime}\033[33m[${level}]\033[0m ${content}"
        ;;
    error)
        echo -e "${curtime}\033[31m[${level}]\033[0m ${content}"
        ;;
    *)
        echo -e "${curtime}\033[34m[${level}]\033[0m ${content}"
        ;;
    esac

}

log debug "这是一个debug信息"
log info "这是一个info信息"
log warning "这是一个warning信息"
log error "这是一个error信息"
log unkown "这是一个unkown信息"

running result

Guess you like

Origin blog.csdn.net/apollo_miracle/article/details/129547220