-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog.sh
More file actions
53 lines (45 loc) · 1.55 KB
/
log.sh
File metadata and controls
53 lines (45 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
#########################################################################
# File Name: log.sh
# Author:陈俊杰
# mail: [email protected]
# Created Time: 2021年08月06日 星期五 16时54分55秒
# 此程序的功能是:
#########################################################################
#日志级别 debug-1, info-2, warn-3, error-4, always-5
LOG_LEVEL=3
#日志文件
LOG_FILE=./log.txt
#调试日志
function log_debug(){
content="[DEBUG] $(date '+%Y-%m-%d %H:%M:%S') $@"
[ $LOG_LEVEL -le 1 ] && echo $content >> $LOG_FILE && echo -e "\033[32m" ${content} "\033[0m"
}
#信息日志
function log_info(){
content="[INFO] $(date '+%Y-%m-%d %H:%M:%S') $@"
[ $LOG_LEVEL -le 2 ] && echo $content >> $LOG_FILE && echo -e "\033[32m" ${content} "\033[0m"
}
#警告日志
function log_warn(){
content="[WARN] $(date '+%Y-%m-%d %H:%M:%S') $@"
[ $LOG_LEVEL -le 3 ] && echo $content >> $LOG_FILE && echo -e "\033[33m" ${content} "\033[0m"
}
#错误日志
function log_err(){
content="[ERROR] $(date '+%Y-%m-%d %H:%M:%S') $@"
[ $LOG_LEVEL -le 4 ] && echo $content >> $LOG_FILE && echo -e "\033[31m" ${content} "\033[0m"
}
#一直都会打印的日志
function log_always(){
content="[ALWAYS] $(date '+%Y-%m-%d %H:%M:%S') $@"
[ $LOG_LEVEL -le 5 ] && echo $content >> $LOG_FILE && echo -e "\033[32m" ${content} "\033[0m"
}
# ta.sh 脚本, 内容如下
#!/bin/bash
source ./log.sh
log_debug "this is debug log..."
log_info "this is info log..."
log_warn "this is warn log..."
log_err "this is error log..."
log_always "this is always log.."