package blog4go

import (
"fmt"
"strings"
)

// LevelType type defined for logging level
// just use int
type LevelType int

const (
// level enum  日志枚举

// TRACE trace level
TRACE LevelType = iota
// DEBUG debug level
DEBUG
// INFO info level
INFO
// WARNING warn level
WARNING
// ERROR error level
ERROR
// CRITICAL critical level
CRITICAL
// UNKNOWN unknown level
UNKNOWN = "UNKNOWN"

// DefaultLevel default level for writers
DefaultLevel = TRACE

// PrefixFormat每个消息头前缀
PrefixFormat = " [%s] " //约定格式
// ColoredPrefixFormat 不同日志级别对应不同的颜色消息
ColoredPrefixFormat = " [\x1b[%dm%s\x1b[0m] " //格式化的颜色

// color enum used in formating color bytes

// NOCOLOR no color
NOCOLOR = 0
// RED red color
RED = 31
// GREEN green color
GREEN = 32
// YELLOW yellow color
YELLOW = 33
// BLUE blue color
BLUE = 34
// GRAY gray color
GRAY = 37
)

var (
// LevelStrings is string present for each level
LevelStrings = [...]string{"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"}

// StringLevels is map, level strings to levels
StringLevels = map[string]LevelType{"TRACE": TRACE, "DEBUG": DEBUG, "INFO": INFO, "WARN": WARNING, "ERROR": ERROR, "CRITICAL": CRITICAL}

// Levels is a slice consist of all levels
Levels = [...]LevelType{TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL}

// Prefix is preformatted level prefix string
// help reduce string formatted burden in realtime logging
Prefix = make(map[LevelType]string)
)

func init() {
initPrefix(false) // preformat level prefix string
}

// initPrefix is designed to preformat level prefix string for each level.
// colored decide whether preformat in colored format or not.
// if colored is true, preformat level prefix string in colored format
func initPrefix(colored bool) {
if colored {
Prefix[TRACE] = fmt.Sprintf(ColoredPrefixFormat, GRAY, TRACE.String())
Prefix[DEBUG] = fmt.Sprintf(ColoredPrefixFormat, GREEN, DEBUG.String())
Prefix[INFO] = fmt.Sprintf(ColoredPrefixFormat, BLUE, INFO.String())
Prefix[WARNING] = fmt.Sprintf(ColoredPrefixFormat, YELLOW, WARNING.String())
Prefix[ERROR] = fmt.Sprintf(ColoredPrefixFormat, RED, ERROR.String())
Prefix[CRITICAL] = fmt.Sprintf(ColoredPrefixFormat, RED, CRITICAL.String())
} else {
Prefix[TRACE] = fmt.Sprintf(PrefixFormat, TRACE.String())
Prefix[DEBUG] = fmt.Sprintf(PrefixFormat, DEBUG.String())
Prefix[INFO] = fmt.Sprintf(PrefixFormat, INFO.String())
Prefix[WARNING] = fmt.Sprintf(PrefixFormat, WARNING.String())
Prefix[ERROR] = fmt.Sprintf(PrefixFormat, ERROR.String())
Prefix[CRITICAL] = fmt.Sprintf(PrefixFormat, CRITICAL.String())
}
}

// valid determines whether a Level instance is valid or not
func (level LevelType) valid() bool {
if TRACE > level || CRITICAL < level {
return false
}
return true
}

// String return string format associate with a Level instance
func (level LevelType) String() string {
if !level.valid() {
return UNKNOWN
}
return LevelStrings[level]
}

// prefix return formatted prefix string associate with a Level instance
func (level LevelType) prefix() string {
return Prefix[level]
}

// LevelFromString return Level according to given string
func LevelFromString(str string) LevelType {
level, ok := StringLevels[strings.ToUpper(str)]
if !ok {
return LevelType(-1)
}
return level
}

level.go的更多相关文章

  1. Java compiler level does not match解决方法

    从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description  Resource Path Location Type Java compiler level d ...

  2. Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead的解决办法

    今天在导入工程进Eclipse的时候竟然出错了,控制台输出的是: [2013-02-04 22:17:13 - takepicture] Android requires compiler compl ...

  3. Android版本和API Level对应关系

    http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version       API ...

  4. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  6. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. Android版本与api Level

    Platform Version API Level VERSION_CODE Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 ...

  8. Selenium通过WebDriver控制IE浏览器出错 Browser zoom level was set to 109%. It should be set to 100%

    错误信息: WebDriverException: Message: Unexpected error launching Internet Explorer. Browser zoom level ...

  9. pythonchallenge 解谜 Level 8

    #-*- coding:utf-8 -*- #代码版本均为python 3.5.1 #Level 7 import bz2 un=b'BZh91AY&SYA\xaf\x82\r\x00\x00 ...

  10. pythonchallenge 解谜 Level 7

    #-*- coding:utf-8 -*- #代码版本均为python 3.5.1 #Level 7 from PIL import Image x_begin, x_end = 0, 609 y_b ...

随机推荐

  1. 恶补web之二:css知识(1)

    css指层叠样式表(Cascading Style Sheets)     样式定义如何显示html元素,样式通常存储在样式表里.把样式添加到html4.0中,是为了解决内容与表现分离的问题.外部样式 ...

  2. Integer 和int 比较

    在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...

  3. java核心卷轴之集合

    1. Iterator 1.1 注意事项 接口的remove方法将删除上次调用next方式时返回的对象,即:remove之前,必须有next(先获取,再删除). 1.2 例一:删除字符串集合中的第一个 ...

  4. 从Freelancer的热门Skill看看你应该学什么?

    以下数据是2012-1-31号数据. Websites, IT & Software: PHP (2402)HTML (1639)SEO(877)MySQL (836)Link Buildin ...

  5. Redis的安装及学习

    最近因为做Chatbot项目需要对于NoSQL数据库进行研究,调研范围包括MongoDB和Redis.本文将介绍Redis在Windows环境的安装及如何利用python来操作Redis数据库. Re ...

  6. kaggle入门项目:Titanic存亡预测(二)数据处理

    原kaggle比赛地址:https://www.kaggle.com/c/titanic 原kernel地址:A Data Science Framework: To Achieve 99% Accu ...

  7. js实现定时器,时间倒计时为0后停止

    <script type="text/javascript"> var orign_time = 1496706400; var leftTime = Date.par ...

  8. Hi,这有一份风控体系建设干货

    互联网.移动互联网.云计算.大数据.人工智能.物联网.区块链等技术已经在人类经济生活中扮演越来越重要的角色,技术给人类带来各种便利的同时,很多企业也饱受"硬币"另一面的伤害,并且形 ...

  9. Python3实现ICMP远控后门(中)之“嗅探”黑科技

    ICMP后门 前言 第一篇:Python3实现ICMP远控后门(上) 第二篇:Python3实现ICMP远控后门(上)_补充篇 在上两篇文章中,详细讲解了ICMP协议,同时实现了一个具备完整功能的pi ...

  10. CSS层叠和继承

    CSS具有两个核心的概念--继承和层叠.一般文本类的属性会被继承,即某个元素的CSS属性会传递给内部嵌套的元素.一个元素可能有一个或者多个样式的来源,当属性发生冲突时,就会根据加载顺序和权重大小决定层 ...