2014-07-26 06:24:27 +02:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-02 09:48:31 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-07-26 06:24:27 +02:00
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2021-03-01 22:08:10 +01:00
|
|
|
"fmt"
|
2019-04-02 09:48:31 +02:00
|
|
|
"io"
|
2014-07-26 06:24:27 +02:00
|
|
|
"os"
|
2021-03-01 22:08:10 +01:00
|
|
|
|
2021-07-24 18:03:58 +02:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2014-07-26 06:24:27 +02:00
|
|
|
)
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// CanColorStdout reports if we can color the Stdout
|
|
|
|
// Although we could do terminal sniffing and the like - in reality
|
|
|
|
// most tools on *nix are happy to display ansi colors.
|
|
|
|
// We will terminal sniff on Windows in console_windows.go
|
|
|
|
var CanColorStdout = true
|
2014-07-26 06:24:27 +02:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// CanColorStderr reports if we can color the Stderr
|
|
|
|
var CanColorStderr = true
|
|
|
|
|
|
|
|
type nopWriteCloser struct {
|
|
|
|
w io.WriteCloser
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
func (n *nopWriteCloser) Write(p []byte) (int, error) {
|
|
|
|
return n.w.Write(p)
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
func (n *nopWriteCloser) Close() error {
|
|
|
|
return nil
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// ConsoleLogger implements LoggerProvider and writes messages to terminal.
|
|
|
|
type ConsoleLogger struct {
|
2019-04-07 02:25:14 +02:00
|
|
|
WriterLogger
|
2019-04-02 09:48:31 +02:00
|
|
|
Stderr bool `json:"stderr"`
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// NewConsoleLogger create ConsoleLogger returning as LoggerProvider.
|
|
|
|
func NewConsoleLogger() LoggerProvider {
|
|
|
|
log := &ConsoleLogger{}
|
2019-04-07 02:25:14 +02:00
|
|
|
log.NewWriterLogger(&nopWriteCloser{
|
2019-04-02 09:48:31 +02:00
|
|
|
w: os.Stdout,
|
|
|
|
})
|
|
|
|
return log
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Init inits connection writer with json config.
|
|
|
|
// json config only need key "level".
|
|
|
|
func (log *ConsoleLogger) Init(config string) error {
|
|
|
|
err := json.Unmarshal([]byte(config), log)
|
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("Unable to parse JSON: %w", err)
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
if log.Stderr {
|
2019-04-07 02:25:14 +02:00
|
|
|
log.NewWriterLogger(&nopWriteCloser{
|
2019-04-02 09:48:31 +02:00
|
|
|
w: os.Stderr,
|
|
|
|
})
|
2014-07-26 06:24:27 +02:00
|
|
|
} else {
|
2019-04-07 02:25:14 +02:00
|
|
|
log.NewWriterLogger(log.out)
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-26 12:53:29 +01:00
|
|
|
// Flush when log should be flushed
|
2019-04-02 09:48:31 +02:00
|
|
|
func (log *ConsoleLogger) Flush() {
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 02:07:07 +02:00
|
|
|
// ReleaseReopen causes the console logger to reconnect to os.Stdout
|
|
|
|
func (log *ConsoleLogger) ReleaseReopen() error {
|
|
|
|
if log.Stderr {
|
|
|
|
log.NewWriterLogger(&nopWriteCloser{
|
|
|
|
w: os.Stderr,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log.NewWriterLogger(&nopWriteCloser{
|
|
|
|
w: os.Stdout,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// GetName returns the default name for this implementation
|
|
|
|
func (log *ConsoleLogger) GetName() string {
|
|
|
|
return "console"
|
2014-07-27 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-26 06:24:27 +02:00
|
|
|
func init() {
|
2019-04-02 09:48:31 +02:00
|
|
|
Register("console", NewConsoleLogger)
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|