github.com/XiaoMi/Gaea@v1.2.5/log/xlog/console.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package xlog
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  	"strconv"
    22  	"time"
    23  )
    24  
    25  // constants of XConsoleLog
    26  const (
    27  	XConsoleLogDefaultlogID = "800000001"
    28  )
    29  
    30  // XConsoleLog is the console logger
    31  type XConsoleLog struct {
    32  	level int
    33  
    34  	skip     int
    35  	hostname string
    36  	service  string
    37  }
    38  
    39  // Brush is pretty string
    40  type Brush func(string) string
    41  
    42  // NewBrush is the constructor of Brush
    43  func NewBrush(color string) Brush {
    44  	pre := "\033["
    45  	reset := "\033[0m"
    46  	return func(text string) string {
    47  		return pre + color + "m" + text + reset
    48  	}
    49  }
    50  
    51  var colors = []Brush{
    52  	NewBrush("1;37"), // white
    53  	NewBrush("1;36"), // debug			cyan
    54  	NewBrush("1;35"), // trace   magenta
    55  	NewBrush("1;31"), // notice      red
    56  	NewBrush("1;33"), // warn    yellow
    57  	NewBrush("1;32"), // fatal			green
    58  	NewBrush("1;34"), //
    59  	NewBrush("1;34"), //
    60  }
    61  
    62  // NewXConsoleLog is the constructor of XConsoleLog
    63  func NewXConsoleLog() XLogger {
    64  	return &XConsoleLog{
    65  		skip: XLogDefSkipNum,
    66  	}
    67  }
    68  
    69  // Init init XConsoleLog
    70  func (p *XConsoleLog) Init(config map[string]string) (err error) {
    71  	level, ok := config["level"]
    72  	if !ok {
    73  		err = fmt.Errorf("init XConsoleLog failed, not found level")
    74  		return
    75  	}
    76  
    77  	service, _ := config["service"]
    78  	if len(service) > 0 {
    79  		p.service = service
    80  	}
    81  
    82  	skip, _ := config["skip"]
    83  	if len(skip) > 0 {
    84  		skipNum, err := strconv.Atoi(skip)
    85  		if err == nil {
    86  			p.skip = skipNum
    87  		}
    88  	}
    89  
    90  	p.level = LevelFromStr(level)
    91  	hostname, _ := os.Hostname()
    92  	p.hostname = hostname
    93  
    94  	return
    95  }
    96  
    97  // SetLevel implements XLogger
    98  func (p *XConsoleLog) SetLevel(level string) {
    99  	p.level = LevelFromStr(level)
   100  }
   101  
   102  // SetSkip implements XLogger
   103  func (p *XConsoleLog) SetSkip(skip int) {
   104  	p.skip = skip
   105  }
   106  
   107  // ReOpen implements XLogger
   108  func (p *XConsoleLog) ReOpen() error {
   109  	return nil
   110  }
   111  
   112  // Warn implements XLogger
   113  func (p *XConsoleLog) Warn(format string, a ...interface{}) error {
   114  	return p.warnx(XConsoleLogDefaultlogID, format, a...)
   115  }
   116  
   117  // Warnx implements XLogger
   118  func (p *XConsoleLog) Warnx(logID, format string, a ...interface{}) error {
   119  	return p.warnx(logID, format, a...)
   120  }
   121  
   122  func (p *XConsoleLog) warnx(logID, format string, a ...interface{}) error {
   123  	if p.level > WarnLevel {
   124  		return nil
   125  	}
   126  
   127  	logText := formatValue(format, a...)
   128  	fun, filename, lineno := getRuntimeInfo(p.skip)
   129  
   130  	color := colors[WarnLevel]
   131  	logText = color(fmt.Sprintf("[%s:%s:%d] %s", fun, filepath.Base(filename), lineno, logText))
   132  
   133  	return p.write(WarnLevel, &logText, logID)
   134  }
   135  
   136  // Fatal implements XLogger
   137  func (p *XConsoleLog) Fatal(format string, a ...interface{}) error {
   138  	return p.fatalx(XConsoleLogDefaultlogID, format, a...)
   139  }
   140  
   141  // Fatalx implements XLogger
   142  func (p *XConsoleLog) Fatalx(logID, format string, a ...interface{}) error {
   143  	return p.fatalx(logID, format, a...)
   144  }
   145  
   146  func (p *XConsoleLog) fatalx(logID, format string, a ...interface{}) error {
   147  	if p.level > FatalLevel {
   148  		return nil
   149  	}
   150  
   151  	logText := formatValue(format, a...)
   152  	fun, filename, lineno := getRuntimeInfo(p.skip)
   153  
   154  	color := colors[FatalLevel]
   155  	logText = color(fmt.Sprintf("[%s:%s:%d] %s", fun, filepath.Base(filename), lineno, logText))
   156  
   157  	return p.write(FatalLevel, &logText, logID)
   158  }
   159  
   160  // Notice implements XLogger
   161  func (p *XConsoleLog) Notice(format string, a ...interface{}) error {
   162  	return p.Noticex(XConsoleLogDefaultlogID, format, a...)
   163  }
   164  
   165  // Noticex implements XLogger
   166  func (p *XConsoleLog) Noticex(logID, format string, a ...interface{}) error {
   167  	if p.level > NoticeLevel {
   168  		return nil
   169  	}
   170  
   171  	logText := formatValue(format, a...)
   172  	return p.write(NoticeLevel, &logText, logID)
   173  }
   174  
   175  // Trace implements XLogger
   176  func (p *XConsoleLog) Trace(format string, a ...interface{}) error {
   177  	return p.tracex(XConsoleLogDefaultlogID, format, a...)
   178  }
   179  
   180  // Tracex implements XLogger
   181  func (p *XConsoleLog) Tracex(logID, format string, a ...interface{}) error {
   182  	return p.tracex(logID, format, a...)
   183  }
   184  
   185  func (p *XConsoleLog) tracex(logID, format string, a ...interface{}) error {
   186  	if p.level > TraceLevel {
   187  		return nil
   188  	}
   189  
   190  	logText := formatValue(format, a...)
   191  	fun, filename, lineno := getRuntimeInfo(p.skip)
   192  
   193  	color := colors[TraceLevel]
   194  	logText = color(fmt.Sprintf("[%s:%s:%d] %s", fun, filepath.Base(filename), lineno, logText))
   195  
   196  	return p.write(TraceLevel, &logText, logID)
   197  }
   198  
   199  // Debug implements XLogger
   200  func (p *XConsoleLog) Debug(format string, a ...interface{}) error {
   201  	return p.debugx(XConsoleLogDefaultlogID, format, a...)
   202  }
   203  
   204  // Debugx implements XLogger
   205  func (p *XConsoleLog) Debugx(logID, format string, a ...interface{}) error {
   206  	return p.debugx(logID, format, a...)
   207  }
   208  
   209  func (p *XConsoleLog) debugx(logID, format string, a ...interface{}) error {
   210  	if p.level > DebugLevel {
   211  		return nil
   212  	}
   213  
   214  	logText := formatValue(format, a...)
   215  	fun, filename, lineno := getRuntimeInfo(p.skip)
   216  
   217  	color := colors[DebugLevel]
   218  	logText = color(fmt.Sprintf("[%s:%s:%d] %s", fun, filepath.Base(filename), lineno, logText))
   219  
   220  	return p.write(DebugLevel, &logText, logID)
   221  }
   222  
   223  // Close implements XLogger
   224  //关闭日志库。注意:如果没有调用Close()关闭日志库的话,将会造成文件句柄泄露
   225  func (p *XConsoleLog) Close() {
   226  }
   227  
   228  // GetHost getter of hostname
   229  func (p *XConsoleLog) GetHost() string {
   230  	return p.hostname
   231  }
   232  
   233  func (p *XConsoleLog) write(level int, msg *string, logID string) error {
   234  	color := colors[level]
   235  	levelText := color(levelTextArray[level])
   236  	time := time.Now().Format("2006-01-02 15:04:05")
   237  
   238  	logText := formatLog(msg, time, p.service, p.hostname, levelText, logID)
   239  	file := os.Stdout
   240  	if level >= WarnLevel {
   241  		file = os.Stderr
   242  	}
   243  
   244  	file.Write([]byte(logText))
   245  	return nil
   246  }