1
0

console.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "runtime"
  19. "time"
  20. )
  21. // brush is a color join function
  22. type brush func(string) string
  23. // newBrush return a fix color Brush
  24. func newBrush(color string) brush {
  25. pre := "\033["
  26. reset := "\033[0m"
  27. return func(text string) string {
  28. return pre + color + "m" + text + reset
  29. }
  30. }
  31. var colors = []brush{
  32. newBrush("1;37"), // Emergency white
  33. newBrush("1;36"), // Alert cyan
  34. newBrush("1;35"), // Critical magenta
  35. newBrush("1;31"), // Error red
  36. newBrush("1;33"), // Warning yellow
  37. newBrush("1;32"), // Notice green
  38. newBrush("1;34"), // Informational blue
  39. newBrush("1;34"), // Debug blue
  40. newBrush("1;34"), // Trace 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(os.Stdout),
  52. Level: LevelTrace,
  53. Colorful: runtime.GOOS != "windows",
  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. err := json.Unmarshal([]byte(jsonConfig), c)
  64. if runtime.GOOS == "windows" {
  65. c.Colorful = false
  66. }
  67. return err
  68. }
  69. // WriteMsg write message in console.
  70. func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
  71. if level > c.Level {
  72. return nil
  73. }
  74. if c.Colorful {
  75. msg = colors[level](msg)
  76. }
  77. c.lg.println(when, msg)
  78. return nil
  79. }
  80. // Destroy implementing method. empty.
  81. func (c *consoleWriter) Destroy() {
  82. }
  83. // Flush implementing method. empty.
  84. func (c *consoleWriter) Flush() {
  85. }
  86. func init() {
  87. Register(AdapterConsole, NewConsole)
  88. }