log
import "github.com/tangoui/tango/service/log"本页由
site/tools/apigen从源码注释生成(按 darwin 构建标签选文件),请勿手改。
Package log sets up application logging on log/slog: records go to stderr and to a log file that rotates by size, and the page can log through ipc (Handler).
The package name shadows the standard library's log; import it under another name if you need both, e.g. applog "github.com/tangoui/tango/service/log".
常量
const (
DefaultFile = "app.log"
DefaultMaxSize = 5 << 20
DefaultKeep = 3
)Defaults for Options.
const Method = "Log.Write"Method is the ipc method Handler registers.
函数
func Handler
func Handler(r *ipc.Router, l *slog.Logger)Handler lets the page log through l (slog.Default() if nil). The page calls
tango.call("Log.Write", "warn", "upload failed", {file: "a.png"})
with a level ("debug", "info", "warn" or "error"; anything else is info), a message and optional attributes. Records carry source=page.
类型
Logger
type Logger struct {
*slog.Logger
// contains filtered or unexported fields
}Logger is a *slog.Logger writing to the console and a rotating file. Shutdown closes the file, so a Logger can be registered as a service.
func New
func New(opts Options) (*Logger, error)New opens the log file and returns a Logger writing text records to it and to the console.
func (*Logger) Path
func (l *Logger) Path() stringPath returns the current log file's path.
func (*Logger) Shutdown
func (l *Logger) Shutdown() errorShutdown closes the log file. Records logged afterwards reach only the console.
Options
type Options struct {
// AppID picks the default Dir: paths.Logs(AppID). Required if Dir is
// empty.
AppID string
// Dir is the directory of the log files; created if missing.
Dir string
// File is the current log file's name in Dir (DefaultFile).
File string
// MaxSize is the size in bytes past which the file rotates
// (DefaultMaxSize).
MaxSize int64
// Keep is how many rotated files (File.1 newest … File.Keep oldest)
// are kept (DefaultKeep).
Keep int
// Level is the minimum level logged (slog.LevelInfo).
Level slog.Leveler
// Console receives a copy of every record (os.Stderr). Use io.Discard
// to log to the file only.
Console io.Writer
// contains filtered or unexported fields
}Options configures New. The zero value of each field picks a default.
RotatingFile
type RotatingFile struct {
// contains filtered or unexported fields
}RotatingFile is an append-only file that, when a write would take it past its maximum size, is renamed to path.1 (path.1 to path.2, and so on, dropping the oldest) and started afresh. Safe for concurrent use.
func OpenRotating
func OpenRotating(path string, maxSize int64, keep int) (*RotatingFile, error)OpenRotating opens (appending) or creates the file at path, creating its directory. maxSize <= 0 means DefaultMaxSize; keep <= 0 means DefaultKeep.
func (*RotatingFile) Close
func (r *RotatingFile) Close() errorClose closes the file. Later writes fail with os.ErrClosed.
func (*RotatingFile) Write
func (r *RotatingFile) Write(p []byte) (int, error)Write appends p, rotating first if p would not fit. A single write larger than the maximum goes into a file of its own.