2014-07-26 09:54:27 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-02 13:18:31 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-07-26 09:54:27 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2021-03-02 02:38:10 +05:30
|
|
|
"fmt"
|
2019-04-02 13:18:31 +05:30
|
|
|
"io"
|
2014-07-26 09:54:27 +05:30
|
|
|
"os"
|
2021-03-02 02:38:10 +05:30
|
|
|
|
2021-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2014-07-26 09:54:27 +05:30
|
|
|
)
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// 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 09:54:27 +05:30
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// CanColorStderr reports if we can color the Stderr
|
|
|
|
var CanColorStderr = true
|
|
|
|
|
|
|
|
type nopWriteCloser struct {
|
|
|
|
w io.WriteCloser
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
func (n *nopWriteCloser) Write(p []byte) (int, error) {
|
|
|
|
return n.w.Write(p)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
func (n *nopWriteCloser) Close() error {
|
|
|
|
return nil
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// ConsoleLogger implements LoggerProvider and writes messages to terminal.
|
|
|
|
type ConsoleLogger struct {
|
2019-04-07 05:55:14 +05:30
|
|
|
WriterLogger
|
2019-04-02 13:18:31 +05:30
|
|
|
Stderr bool `json:"stderr"`
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// NewConsoleLogger create ConsoleLogger returning as LoggerProvider.
|
|
|
|
func NewConsoleLogger() LoggerProvider {
|
|
|
|
log := &ConsoleLogger{}
|
2019-04-07 05:55:14 +05:30
|
|
|
log.NewWriterLogger(&nopWriteCloser{
|
2019-04-02 13:18:31 +05:30
|
|
|
w: os.Stdout,
|
|
|
|
})
|
|
|
|
return log
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// 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-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Unable to parse JSON: %w", err)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
2019-04-02 13:18:31 +05:30
|
|
|
if log.Stderr {
|
2019-04-07 05:55:14 +05:30
|
|
|
log.NewWriterLogger(&nopWriteCloser{
|
2019-04-02 13:18:31 +05:30
|
|
|
w: os.Stderr,
|
|
|
|
})
|
2014-07-26 09:54:27 +05:30
|
|
|
} else {
|
2019-04-07 05:55:14 +05:30
|
|
|
log.NewWriterLogger(log.out)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-25 14:50:50 +05:30
|
|
|
// Content returns the content accumulated in the content provider
|
|
|
|
func (log *ConsoleLogger) Content() (string, error) {
|
|
|
|
return "", fmt.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
2016-11-26 17:23:29 +05:30
|
|
|
// Flush when log should be flushed
|
2019-04-02 13:18:31 +05:30
|
|
|
func (log *ConsoleLogger) Flush() {
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2020-07-06 05:37:07 +05:30
|
|
|
// 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 13:18:31 +05:30
|
|
|
// GetName returns the default name for this implementation
|
|
|
|
func (log *ConsoleLogger) GetName() string {
|
|
|
|
return "console"
|
2014-07-27 04:07:18 +05:30
|
|
|
}
|
|
|
|
|
2014-07-26 09:54:27 +05:30
|
|
|
func init() {
|
2019-04-02 13:18:31 +05:30
|
|
|
Register("console", NewConsoleLogger)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|