高热共公日志库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.5 KiB

  1. // Copyright 2014 beego Author. 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. package logs
  15. import (
  16. "encoding/json"
  17. "os"
  18. "strings"
  19. "time"
  20. "github.com/shiena/ansicolor"
  21. )
  22. // brush is a color join function
  23. type brush func(string) string
  24. // newBrush return a fix color Brush
  25. func newBrush(color string) brush {
  26. pre := "\033["
  27. reset := "\033[0m"
  28. return func(text string) string {
  29. return pre + color + "m" + text + reset
  30. }
  31. }
  32. var colors = []brush{
  33. newBrush("1;37"), // Emergency white
  34. newBrush("1;36"), // Alert cyan
  35. newBrush("1;35"), // Critical magenta
  36. newBrush("1;31"), // Error red
  37. newBrush("1;33"), // Warning yellow
  38. newBrush("1;32"), // Notice green
  39. newBrush("1;34"), // Informational blue
  40. newBrush("1;44"), // Debug Background blue
  41. }
  42. // consoleWriter implements LoggerInterface and writes messages to terminal.
  43. type consoleWriter struct {
  44. lg *logWriter
  45. Level int `json:"level"`
  46. Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
  47. }
  48. // NewConsole create ConsoleWriter returning as LoggerInterface.
  49. func NewConsole() Logger {
  50. cw := &consoleWriter{
  51. lg: newLogWriter(ansicolor.NewAnsiColorWriter(os.Stdout)),
  52. Level: LevelDebug,
  53. Colorful: true,
  54. }
  55. return cw
  56. }
  57. // Init init console logger.
  58. // jsonConfig like '{"level":LevelTrace}'.
  59. func (c *consoleWriter) Init(jsonConfig string) error {
  60. if len(jsonConfig) == 0 {
  61. return nil
  62. }
  63. return json.Unmarshal([]byte(jsonConfig), c)
  64. }
  65. // WriteMsg write message in console.
  66. func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
  67. if level > c.Level {
  68. return nil
  69. }
  70. if c.Colorful {
  71. msg = strings.Replace(msg, levelPrefix[level], colors[level](levelPrefix[level]), 1)
  72. }
  73. c.lg.writeln(when, msg)
  74. return nil
  75. }
  76. // Destroy implementing method. empty.
  77. func (c *consoleWriter) Destroy() {
  78. }
  79. // Flush implementing method. empty.
  80. func (c *consoleWriter) Flush() {
  81. }
  82. func init() {
  83. Register(AdapterConsole, NewConsole)
  84. }