console.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package log
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "log"
  9. "os"
  10. "runtime"
  11. )
  12. type Brush func(string) string
  13. func NewBrush(color string) Brush {
  14. pre := "\033["
  15. reset := "\033[0m"
  16. return func(text string) string {
  17. return pre + color + "m" + text + reset
  18. }
  19. }
  20. var (
  21. Red = NewBrush("1;31")
  22. Purple = NewBrush("1;35")
  23. Yellow = NewBrush("1;33")
  24. Green = NewBrush("1;32")
  25. Blue = NewBrush("1;34")
  26. Cyan = NewBrush("1;36")
  27. colors = []Brush{
  28. Cyan, // Trace cyan
  29. Blue, // Debug blue
  30. Green, // Info green
  31. Yellow, // Warn yellow
  32. Red, // Error red
  33. Purple, // Critical purple
  34. Red, // Fatal red
  35. }
  36. consoleWriter = &ConsoleWriter{lg: log.New(os.Stdout, "", 0),
  37. Level: TRACE}
  38. )
  39. // ConsoleWriter implements LoggerInterface and writes messages to terminal.
  40. type ConsoleWriter struct {
  41. lg *log.Logger
  42. Level int `json:"level"`
  43. Formatting bool `json:"formatting"`
  44. }
  45. // create ConsoleWriter returning as LoggerInterface.
  46. func NewConsole() LoggerInterface {
  47. return &ConsoleWriter{
  48. lg: log.New(os.Stderr, "", log.Ldate|log.Ltime),
  49. Level: TRACE,
  50. Formatting: true,
  51. }
  52. }
  53. func (cw *ConsoleWriter) Init(config string) error {
  54. return json.Unmarshal([]byte(config), cw)
  55. }
  56. func (cw *ConsoleWriter) WriteMsg(msg string, skip, level int) error {
  57. if cw.Level > level {
  58. return nil
  59. }
  60. if runtime.GOOS == "windows" || !cw.Formatting {
  61. cw.lg.Println(msg)
  62. } else {
  63. cw.lg.Println(colors[level](msg))
  64. }
  65. return nil
  66. }
  67. func (_ *ConsoleWriter) Flush() {
  68. }
  69. func (_ *ConsoleWriter) Destroy() {
  70. }
  71. func printConsole(level int, msg string) {
  72. consoleWriter.WriteMsg(msg, 0, level)
  73. }
  74. func printfConsole(level int, format string, v ...interface{}) {
  75. consoleWriter.WriteMsg(fmt.Sprintf(format, v...), 0, level)
  76. }
  77. // ConsoleTrace prints to stdout using TRACE colors
  78. func ConsoleTrace(s string) {
  79. printConsole(TRACE, s)
  80. }
  81. // ConsoleTracef prints a formatted string to stdout using TRACE colors
  82. func ConsoleTracef(format string, v ...interface{}) {
  83. printfConsole(TRACE, format, v...)
  84. }
  85. // ConsoleDebug prints to stdout using DEBUG colors
  86. func ConsoleDebug(s string) {
  87. printConsole(DEBUG, s)
  88. }
  89. // ConsoleDebugf prints a formatted string to stdout using DEBUG colors
  90. func ConsoleDebugf(format string, v ...interface{}) {
  91. printfConsole(DEBUG, format, v...)
  92. }
  93. // ConsoleInfo prints to stdout using INFO colors
  94. func ConsoleInfo(s string) {
  95. printConsole(INFO, s)
  96. }
  97. // ConsoleInfof prints a formatted string to stdout using INFO colors
  98. func ConsoleInfof(format string, v ...interface{}) {
  99. printfConsole(INFO, format, v...)
  100. }
  101. // ConsoleWarn prints to stdout using WARN colors
  102. func ConsoleWarn(s string) {
  103. printConsole(WARN, s)
  104. }
  105. // ConsoleWarnf prints a formatted string to stdout using WARN colors
  106. func ConsoleWarnf(format string, v ...interface{}) {
  107. printfConsole(WARN, format, v...)
  108. }
  109. // ConsoleError prints to stdout using ERROR colors
  110. func ConsoleError(s string) {
  111. printConsole(ERROR, s)
  112. }
  113. // ConsoleErrorf prints a formatted string to stdout using ERROR colors
  114. func ConsoleErrorf(format string, v ...interface{}) {
  115. printfConsole(ERROR, format, v...)
  116. }
  117. // ConsoleFatal prints to stdout using FATAL colors
  118. func ConsoleFatal(s string) {
  119. printConsole(FATAL, s)
  120. os.Exit(1)
  121. }
  122. // ConsoleFatalf prints a formatted string to stdout using FATAL colors
  123. func ConsoleFatalf(format string, v ...interface{}) {
  124. printfConsole(FATAL, format, v...)
  125. os.Exit(1)
  126. }
  127. func init() {
  128. Register("console", NewConsole)
  129. }