Code Monkey home page Code Monkey logo

sailanchor's Introduction

⛵SailAnchor⚓

This is a log collector for shell scripts, like the ⚓anchor of the ⛵ sail (shell).

⛵展示

MobaXterm

MobaXterm

Xshell

xshell

Windows Terminal

image-20220112101312655

⛵前言

这个脚本是日志收集器,主要用来收集自己写的一些shell脚本中产生的日志,以便于快于定位和解决问题。

不然就像在茫茫大海中,毫无目标。如果在航行中,通过锚(Anchor)就可以进行定位。

本脚本也参考了一些其他优秀脚本的思路,修改后更加贴合自己的要求。

⛵特点

  • 可以使用(debug/info/notice/error/warn)标注不同的信息。并且可以配置不同的颜色,以便更快区别信息。
  • 可以定位错误的Trace Stack。
  • 设置日志输出的等级。(屏蔽和限制等级不进行输出)。
  • 可以同时输出到终端与写入文件。
  • 日志输出错误,可以进行report_capsize后续不再继续输出。

⛵使用

在你的脚本文件开头,加入source ./SailAnchor.sh

在关键的位置加入(debug/info/notice/error/warn)等函数提示信息。

例如:

#!/bin/bash

source ./SailAnchor.sh

notice Sailing
your_command
if [ $? = 0 ];then
  info Command downwind, succeeded.

else
  error Command capsize, failed!
fi

shadow-zoom

或者使用快速判断函数wave()

这里推荐一个使用方法,类似于:./logger.sh > anchors.log 2>&1

#!/bin/bash

source ./SailAnchor.sh

notice Sailing
your_command
wave $? your_command

shadow-zoom

⚓配置

⚓level

LEVEL Value Functions
DEBUG 0 debug
INFO 1 info, information
NOTICE 2 notice, notification
WARNING 3 warn, warning
ERROR 4 error, iceberg

⚓全局变量

Variable Name Description Default
SAILOR_DATE_FORMAT 日期格式化 '%Y/%m/%d %H:%M:%S'
SAILOR_LEVEL 小于SAILOR_LEVEL该值的信息不显示 1
SAILOR_STD_ERROR_LEVEL 大于SAILOR_LEVEL的不再显示 4
SAILOR_DEBUG_COLOR DEBUG信息颜色 3 (斜体,不同终端不同)
SAILOR_INFO_COLOR INFO信息颜色 "" (默认颜色)
SAILOR_NOTICE_COLOR NOTICE信息颜色 36 (cyan)
SAILOR_WARNING_COLOR WARNING信息颜色 33 (yellow)
SAILOR_ERROR_COLOR ERROR信息颜色 31 (red)
SAILOR_COLOR never/auto/always auto
SAILOR_LEVELS 5个等级名称 ("DEBUG" "INFO" "NOTICE" "WARNING" "ERROR")
SAILOR_SHOW_WELCOME 是否显示欢迎 1
SAILOR_SHOW_TIME 是否显示时间 1
SAILOR_SHOW_FILE 是否显示问位置 1
SAILOR_SHOW_LEVEL 是否显示等级 1
SAILOR_SHOW_PID 是否显示PID 0(默认不显示)
SAILOR_ERROR_RETURN_CODE iceberg/error返回码 100
SAILOR_ERROR_TRACE 是否显示错误信息 iceberg/error 1
GLOBAL_FAIL_ANCHOR 全局执行结果标志位 ./iceberg.anc
SHELL_NAME shell脚本名称 SailAnchor.sh
ANCHOR_FILE 日志名称 anchors.log
SAILOR_VSRSION 版本信息 v0.5.2

⚓颜色

关于颜色,可以参考标准定义Standard ECMA-48 (p61, p62).

Normal are:

Number Color definition
30 black display
31 red display
32 green display
33 yellow display
34 blue display
35 magenta display
36 cyan display
37 white display
40 black background
41 red background
42 green background
43 yellow background
44 blue background
45 magenta background
46 cyan background
47 white background

可以同时设置显示(字母颜色)和背景。例如,如果使用红色背景和白色正面颜色进行错误输出,请设置:

SAILOR_ERROR_COLOR="37;41"

⚓架构

struct_v050

Function Name Description
_sailor_time 定位时间
_sailor_site 定位位置
_sailor_level 定位等级
_get_level 获取等级
_weigh_anchor 清除iceberg锚
horn 标识信息
diary 输出到文本,不到终端
clear_diary 清除日志
call 输出到文本以及终端
blow 吹起号角:结合horn使用
_sailor 丢下锚:定位日志问题
debug 输出DEBUG信息
info 输出INFO信息
notice,notification 输出NOTICE信息
warn,warning 输出WARNING信息
error,iceberg 输出ERROR信息,默认带Trace Back Stack
wave 快速错误判断的函数,第一参数必须是$?,第二个参数为操作
welcome 使用脚本的欢迎信息,包含版本信息
step [report]输出步骤信息。注意格式为:[数字] "步骤信息"或者"步骤信息"。使用数字时,可以使得后续步骤从此开始。
before_sail [report]脚本执行前信息
after_sail [report]脚本执行后信息
report_capsize [report]报告错误信息
report_arrival [report]报告成功信息

函数使用示例

基本函数可以使用如下:

debug "This is a debug"
info "This is a info"
notice "This is a notice"
Warn "This is a Warn"
error "This is a error"

step 1 TestCommand1
TestCommand
wave $? "TestCommand"

step TestCommand2
if [ $? = 0 ];then
  info Command downwind, succeeded.
else
  error Command capsize, failed!
fi

⚓注意

  • 保证SHELL_NAME与实际脚本的名称一致。

  • 脚本尽量不要给予+x权限,通过source方式去加载。

  • 尽量使用debug,info,notice,warn,error等具有信息标签的日志输出函数。日志格式统一。

  • step,before_sail,after_sail,report_capsize,report_arrival等报告信息函数可以在关键信息报告或者测试中使用!会破坏一定日志格式。

  • 使用report_capsize会屏蔽后续错误日志的输出!

  • 如果要修改参数,可以在source SailAnchor之前进行赋值,如

    LOG_FILE=../logs/demo.log
    SAILOR_SHOW_WELCOME=0
    source ./SailAnchor.sh
  • 实际运行中可能存在一些问题,后续有待调整!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.