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.

73 lines
1.5 KiB

  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. "log"
  8. "os"
  9. "runtime"
  10. )
  11. type Brush func(string) string
  12. func NewBrush(color string) Brush {
  13. pre := "\033["
  14. reset := "\033[0m"
  15. return func(text string) string {
  16. return pre + color + "m" + text + reset
  17. }
  18. }
  19. var colors = []Brush{
  20. NewBrush("1;36"), // Trace cyan
  21. NewBrush("1;34"), // Debug blue
  22. NewBrush("1;32"), // Info green
  23. NewBrush("1;33"), // Warn yellow
  24. NewBrush("1;31"), // Error red
  25. NewBrush("1;35"), // Critical purple
  26. NewBrush("1;31"), // Fatal red
  27. }
  28. // ConsoleWriter implements LoggerInterface and writes messages to terminal.
  29. type ConsoleWriter struct {
  30. lg *log.Logger
  31. Level int `json:"level"`
  32. }
  33. // create ConsoleWriter returning as LoggerInterface.
  34. func NewConsole() LoggerInterface {
  35. return &ConsoleWriter{
  36. lg: log.New(os.Stdout, "", log.Ldate|log.Ltime),
  37. Level: TRACE,
  38. }
  39. }
  40. func (cw *ConsoleWriter) Init(config string) error {
  41. return json.Unmarshal([]byte(config), cw)
  42. }
  43. func (cw *ConsoleWriter) WriteMsg(msg string, skip, level int) error {
  44. if cw.Level > level {
  45. return nil
  46. }
  47. if runtime.GOOS == "windows" {
  48. cw.lg.Println(msg)
  49. } else {
  50. cw.lg.Println(colors[level](msg))
  51. }
  52. return nil
  53. }
  54. func (_ *ConsoleWriter) Flush() {
  55. }
  56. func (_ *ConsoleWriter) Destroy() {
  57. }
  58. func init() {
  59. Register("console", NewConsole)
  60. }