console.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  44. // create ConsoleWriter returning as LoggerInterface.
  45. func NewConsole() LoggerInterface {
  46. return &ConsoleWriter{
  47. lg: log.New(os.Stderr, "", log.Ldate|log.Ltime),
  48. Level: TRACE,
  49. }
  50. }
  51. func (cw *ConsoleWriter) Init(config string) error {
  52. return json.Unmarshal([]byte(config), cw)
  53. }
  54. func (cw *ConsoleWriter) WriteMsg(msg string, skip, level int) error {
  55. if cw.Level > level {
  56. return nil
  57. }
  58. if runtime.GOOS == "windows" {
  59. cw.lg.Println(msg)
  60. } else {
  61. cw.lg.Println(colors[level](msg))
  62. }
  63. return nil
  64. }
  65. func (_ *ConsoleWriter) Flush() {
  66. }
  67. func (_ *ConsoleWriter) Destroy() {
  68. }
  69. func printConsole(level int, msg string) {
  70. consoleWriter.WriteMsg(msg, 0, level)
  71. }
  72. func printfConsole(level int, format string, v ...interface{}) {
  73. consoleWriter.WriteMsg(fmt.Sprintf(format, v...), 0, level)
  74. }
  75. // ConsoleTrace prints to stdout using TRACE colors
  76. func ConsoleTrace(s string) {
  77. printConsole(TRACE, s)
  78. }
  79. // ConsoleTracef prints a formatted string to stdout using TRACE colors
  80. func ConsoleTracef(format string, v ...interface{}) {
  81. printfConsole(TRACE, format, v...)
  82. }
  83. // ConsoleDebug prints to stdout using DEBUG colors
  84. func ConsoleDebug(s string) {
  85. printConsole(DEBUG, s)
  86. }
  87. // ConsoleDebugf prints a formatted string to stdout using DEBUG colors
  88. func ConsoleDebugf(format string, v ...interface{}) {
  89. printfConsole(DEBUG, format, v...)
  90. }
  91. // ConsoleInfo prints to stdout using INFO colors
  92. func ConsoleInfo(s string) {
  93. printConsole(INFO, s)
  94. }
  95. // ConsoleInfof prints a formatted string to stdout using INFO colors
  96. func ConsoleInfof(format string, v ...interface{}) {
  97. printfConsole(INFO, format, v...)
  98. }
  99. // ConsoleWarn prints to stdout using WARN colors
  100. func ConsoleWarn(s string) {
  101. printConsole(WARN, s)
  102. }
  103. // ConsoleWarnf prints a formatted string to stdout using WARN colors
  104. func ConsoleWarnf(format string, v ...interface{}) {
  105. printfConsole(WARN, format, v...)
  106. }
  107. // ConsoleError prints to stdout using ERROR colors
  108. func ConsoleError(s string) {
  109. printConsole(ERROR, s)
  110. }
  111. // ConsoleErrorf prints a formatted string to stdout using ERROR colors
  112. func ConsoleErrorf(format string, v ...interface{}) {
  113. printfConsole(ERROR, format, v...)
  114. }
  115. // ConsoleFatal prints to stdout using FATAL colors
  116. func ConsoleFatal(s string) {
  117. printConsole(FATAL, s)
  118. os.Exit(1)
  119. }
  120. // ConsoleFatalf prints a formatted string to stdout using FATAL colors
  121. func ConsoleFatalf(format string, v ...interface{}) {
  122. printfConsole(FATAL, format, v...)
  123. os.Exit(1)
  124. }
  125. func init() {
  126. Register("console", NewConsole)
  127. }