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.

console.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package log
  6. import (
  7. "fmt"
  8. "io"
  9. "os"
  10. "code.gitea.io/gitea/modules/json"
  11. )
  12. // CanColorStdout reports if we can color the Stdout
  13. // Although we could do terminal sniffing and the like - in reality
  14. // most tools on *nix are happy to display ansi colors.
  15. // We will terminal sniff on Windows in console_windows.go
  16. var CanColorStdout = true
  17. // CanColorStderr reports if we can color the Stderr
  18. var CanColorStderr = true
  19. type nopWriteCloser struct {
  20. w io.WriteCloser
  21. }
  22. func (n *nopWriteCloser) Write(p []byte) (int, error) {
  23. return n.w.Write(p)
  24. }
  25. func (n *nopWriteCloser) Close() error {
  26. return nil
  27. }
  28. // ConsoleLogger implements LoggerProvider and writes messages to terminal.
  29. type ConsoleLogger struct {
  30. WriterLogger
  31. Stderr bool `json:"stderr"`
  32. }
  33. // NewConsoleLogger create ConsoleLogger returning as LoggerProvider.
  34. func NewConsoleLogger() LoggerProvider {
  35. log := &ConsoleLogger{}
  36. log.NewWriterLogger(&nopWriteCloser{
  37. w: os.Stdout,
  38. })
  39. return log
  40. }
  41. // Init inits connection writer with json config.
  42. // json config only need key "level".
  43. func (log *ConsoleLogger) Init(config string) error {
  44. err := json.Unmarshal([]byte(config), log)
  45. if err != nil {
  46. return fmt.Errorf("Unable to parse JSON: %v", err)
  47. }
  48. if log.Stderr {
  49. log.NewWriterLogger(&nopWriteCloser{
  50. w: os.Stderr,
  51. })
  52. } else {
  53. log.NewWriterLogger(log.out)
  54. }
  55. return nil
  56. }
  57. // Content returns the content accumulated in the content provider
  58. func (log *ConsoleLogger) Content() (string, error) {
  59. return "", fmt.Errorf("not supported")
  60. }
  61. // Flush when log should be flushed
  62. func (log *ConsoleLogger) Flush() {
  63. }
  64. // ReleaseReopen causes the console logger to reconnect to os.Stdout
  65. func (log *ConsoleLogger) ReleaseReopen() error {
  66. if log.Stderr {
  67. log.NewWriterLogger(&nopWriteCloser{
  68. w: os.Stderr,
  69. })
  70. } else {
  71. log.NewWriterLogger(&nopWriteCloser{
  72. w: os.Stdout,
  73. })
  74. }
  75. return nil
  76. }
  77. // GetName returns the default name for this implementation
  78. func (log *ConsoleLogger) GetName() string {
  79. return "console"
  80. }
  81. func init() {
  82. Register("console", NewConsoleLogger)
  83. }