forked from External/grumble
Make OpenFile a factory function instead of a method on LogTarget. Use this to initialize the default logging target
This commit is contained in:
parent
dd6f383d3e
commit
9c082d3516
2 changed files with 12 additions and 15 deletions
|
|
@ -37,7 +37,7 @@ func main() {
|
||||||
dataDir.Close()
|
dataDir.Close()
|
||||||
|
|
||||||
// Set up logging
|
// Set up logging
|
||||||
err = logtarget.Default.OpenFile(Args.LogPath)
|
logtarget.Default, err = logtarget.OpenFile(Args.LogPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to open log file (%v): %v", Args.LogPath, err)
|
fmt.Fprintf(os.Stderr, "Unable to open log file (%v): %v", Args.LogPath, err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ import (
|
||||||
type LogTarget interface {
|
type LogTarget interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
|
|
||||||
OpenFile(string) error
|
|
||||||
Rotate() error
|
Rotate() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,8 +31,17 @@ type fileLogTarget struct {
|
||||||
|
|
||||||
var Default LogTarget
|
var Default LogTarget
|
||||||
|
|
||||||
func init() {
|
// OpenFile creates a LogTarget pointing to a log file
|
||||||
Default = &fileLogTarget{}
|
// and returns it.
|
||||||
|
// This method will open the file in append-only mode.
|
||||||
|
func OpenFile(fileName string) (t LogTarget, err error) {
|
||||||
|
target := &fileLogTarget{}
|
||||||
|
target.logfn = fileName
|
||||||
|
target.file, err = os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0650)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return target, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes a log message to all registered io.Writers
|
// Write writes a log message to all registered io.Writers
|
||||||
|
|
@ -58,17 +66,6 @@ func (target *fileLogTarget) Write(in []byte) (int, error) {
|
||||||
return len(in), nil
|
return len(in), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenFile opens the main log file for writing.
|
|
||||||
// This method will open the file in append-only mode.
|
|
||||||
func (target *fileLogTarget) OpenFile(fn string) (err error) {
|
|
||||||
target.logfn = fn
|
|
||||||
target.file, err = os.OpenFile(target.logfn, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0650)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rotate rotates the current log file.
|
// Rotate rotates the current log file.
|
||||||
// This method holds a lock while rotating the log file,
|
// This method holds a lock while rotating the log file,
|
||||||
// and all log writes will be held back until the rotation
|
// and all log writes will be held back until the rotation
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue