Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config console log to write to stderr #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/LogInjectionExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
log.AddFilter("file", l4g.FINE, flw)

// Log some experimental messages
for _,message := range(user_input) {
for _, message := range user_input {
log.Info(message)
}

Expand All @@ -51,7 +51,7 @@ func main() {
// Close the log
log.Close()

// Print what was logged to the file
// Print what was logged to the file
fd, err := os.Open(filename)
if err != nil {
fmt.Printf("Error reopening file: %s", filename)
Expand Down
23 changes: 19 additions & 4 deletions filelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package log4go
import (
"fmt"
"os"
"time"
"strings"
"time"
)

// This log writer sends output to a file
Expand Down Expand Up @@ -41,7 +41,7 @@ type FileLogWriter struct {
maxbackup int

// Sanitize newlines to prevent log injection
sanitize bool
sanitize bool
}

// This is the FileLogWriter's output method
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewFileLogWriter(fname string, rotate bool, daily bool) *FileLogWriter {
sanitize: false, // set to false so as not to break compatibility
}
// open the file for the first time
if err := w.intRotate(); err != nil {
if err := w.open(); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.filename, err)
return nil
}
Expand Down Expand Up @@ -192,6 +192,10 @@ func (w *FileLogWriter) intRotate() error {
}
}

return w.open()
}

func (w *FileLogWriter) open() error {
// Open the log file
fd, err := os.OpenFile(w.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
Expand All @@ -206,9 +210,20 @@ func (w *FileLogWriter) intRotate() error {
w.daily_opendate = now.Day()

// initialize rotation values
// TODO: determine the initial lines
w.maxlines_curlines = 0
w.maxsize_cursize = 0
if err = w.initFileSize(); err != nil {
return err
}
return nil
}

func (w *FileLogWriter) initFileSize() error {
stat, err := w.file.Stat()
if err != nil {
return err
}
w.maxsize_cursize = int(stat.Size())
return nil
}

Expand Down
19 changes: 13 additions & 6 deletions jsonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ConsoleConfig struct {
Enable bool `json:"enable"`
Level string `json:"level"`
Pattern string `json:"pattern"`
Stderr bool `json:"stderr"`
}

type FileConfig struct {
Expand All @@ -34,11 +35,12 @@ type FileConfig struct {
// Recommended: "[%D %T] [%C] [%L] (%S) %M"//
Pattern string `json:"pattern"`

Rotate bool `json:"rotate"`
Maxsize string `json:"maxsize"` // \d+[KMG]? Suffixes are in terms of 2**10
Maxlines string `json:"maxlines"` //\d+[KMG]? Suffixes are in terms of thousands
Daily bool `json:"daily"` //Automatically rotates by day
Sanitize bool `json:"sanitize"` //Sanitize newlines to prevent log injection
Rotate bool `json:"rotate"`
Maxsize string `json:"maxsize"` // \d+[KMG]? Suffixes are in terms of 2**10
Maxlines string `json:"maxlines"` //\d+[KMG]? Suffixes are in terms of thousands
Daily bool `json:"daily"` //Automatically rotates by day
Sanitize bool `json:"sanitize"` //Sanitize newlines to prevent log injection
MaxBackup int `json:"maxBackup"`
}

type SocketConfig struct {
Expand Down Expand Up @@ -155,7 +157,7 @@ func jsonToConsoleLogWriter(filename string, cf *ConsoleConfig) (*ConsoleLogWrit
return nil, true
}

clw := NewConsoleLogWriter()
clw := NewConsoleLogWriter(cf.Stderr)
clw.SetFormat(format)

return clw, true
Expand All @@ -169,6 +171,7 @@ func jsonToFileLogWriter(filename string, ff *FileConfig) (*FileLogWriter, bool)
daily := false
rotate := false
sanitize := false
maxbackup := 999

if len(ff.Filename) > 0 {
file = ff.Filename
Expand All @@ -182,6 +185,9 @@ func jsonToFileLogWriter(filename string, ff *FileConfig) (*FileLogWriter, bool)
if len(ff.Maxsize) > 0 {
maxsize = strToNumSuffix(strings.Trim(ff.Maxsize, " \r\n"), 1024)
}
if ff.MaxBackup > 0 {
maxbackup = ff.MaxBackup
}
daily = ff.Daily
rotate = ff.Rotate
sanitize = ff.Sanitize
Expand All @@ -195,6 +201,7 @@ func jsonToFileLogWriter(filename string, ff *FileConfig) (*FileLogWriter, bool)
flw.SetRotateLines(maxlines)
flw.SetRotateSize(maxsize)
flw.SetSanitize(sanitize)
flw.SetRotateMaxBackup(maxbackup)
return flw, true
}

Expand Down
4 changes: 2 additions & 2 deletions log4go.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ func NewLogger() Logger {
func NewConsoleLogger(lvl Level) Logger {
os.Stderr.WriteString("warning: use of deprecated NewConsoleLogger\n")
return Logger{
"stdout": &Filter{lvl, NewConsoleLogWriter(), "DEFAULT"},
"stdout": &Filter{lvl, NewConsoleLogWriter(false), "DEFAULT"},
}
}

// Create a new logger with a "stdout" filter configured to send log messages at
// or above lvl to standard output.
func NewDefaultLogger(lvl Level) Logger {
return Logger{
"stdout": &Filter{lvl, NewConsoleLogWriter(), "DEFAULT"},
"stdout": &Filter{lvl, NewConsoleLogWriter(false), "DEFAULT"},
}
}

Expand Down
40 changes: 21 additions & 19 deletions log4go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ var logRecordWriteTests = []struct {
}

func TestConsoleLogWriter(t *testing.T) {
console := new(ConsoleLogWriter)
console := &ConsoleLogWriter{
format: "[%T %D] [%L] %M",
w: make(chan *LogRecord, LogBufferLength),
}

r, w := io.Pipe()
go console.run(w)
Expand All @@ -153,7 +156,7 @@ func TestFileLogWriter(t *testing.T) {
}(LogBufferLength)
LogBufferLength = 0

w := NewFileLogWriter(testLogFile, false)
w := NewFileLogWriter(testLogFile, false, false)
if w == nil {
t.Fatalf("Invalid return: w should not be nil")
}
Expand All @@ -176,7 +179,7 @@ func TestXMLLogWriter(t *testing.T) {
}(LogBufferLength)
LogBufferLength = 0

w := NewXMLLogWriter(testLogFile, false)
w := NewXMLLogWriter(testLogFile, false, false)
if w == nil {
t.Fatalf("Invalid return: w should not be nil")
}
Expand Down Expand Up @@ -210,7 +213,7 @@ func TestLogger(t *testing.T) {

//func (l *Logger) AddFilter(name string, level int, writer LogWriter) {}
l := make(Logger)
l.AddFilter("stdout", DEBUG, NewConsoleLogWriter())
l.AddFilter("stdout", DEBUG, NewConsoleLogWriter(false))
if lw, exist := l["stdout"]; lw == nil || exist != true {
t.Fatalf("AddFilter produced invalid logger (DNE or nil)")
}
Expand Down Expand Up @@ -261,7 +264,7 @@ func TestLogOutput(t *testing.T) {
l := make(Logger)

// Delete and open the output log without a timestamp (for a constant md5sum)
l.AddFilter("file", FINEST, NewFileLogWriter(testLogFile, false).SetFormat("[%L] %M"))
l.AddFilter("file", FINEST, NewFileLogWriter(testLogFile, false, false).SetFormat("[%L] %M"))
defer os.Remove(testLogFile)

// Send some log messages
Expand Down Expand Up @@ -356,17 +359,17 @@ func TestXMLConfig(t *testing.T) {
fmt.Fprintln(fd, " <level>FINEST</level>")
fmt.Fprintln(fd, " <property name=\"filename\">test.log</property>")
fmt.Fprintln(fd, " <!--")
fmt.Fprintln(fd, " %T - Time (15:04:05 MST)")
fmt.Fprintln(fd, " %t - Time (15:04)")
fmt.Fprintln(fd, " %D - Date (2006/01/02)")
fmt.Fprintln(fd, " %d - Date (01/02/06)")
fmt.Fprintln(fd, " %L - Level (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT)")
fmt.Fprintln(fd, " %S - Source")
fmt.Fprintln(fd, " %M - Message")
fmt.Fprintf(fd, " %%T - Time (15:04:05 MST)\n")
fmt.Fprintf(fd, " %%t - Time (15:04)\n")
fmt.Fprintf(fd, " %%D - Date (2006/01/02)\n")
fmt.Fprintf(fd, " %%d - Date (01/02/06)\n")
fmt.Fprintf(fd, " %%L - Level (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT)\n")
fmt.Fprintf(fd, " %%S - Source\n")
fmt.Fprintf(fd, " %%M - Message\n")
fmt.Fprintln(fd, " It ignores unknown format strings (and removes them)")
fmt.Fprintln(fd, " Recommended: \"[%D %T] [%L] (%S) %M\"")
fmt.Fprintf(fd, " Recommended: \"[%%D %%T] [%%L] (%%S) %%M\"\n")
fmt.Fprintln(fd, " -->")
fmt.Fprintln(fd, " <property name=\"format\">[%D %T] [%L] (%S) %M</property>")
fmt.Fprintf(fd, " <property name=\"format\">[%%D %%T] [%%L] (%%S) %%M</property>\n")
fmt.Fprintln(fd, " <property name=\"rotate\">false</property> <!-- true enables log rotation, otherwise append -->")
fmt.Fprintln(fd, " <property name=\"maxsize\">0M</property> <!-- \\d+[KMG]? Suffixes are in terms of 2**10 -->")
fmt.Fprintln(fd, " <property name=\"maxlines\">0K</property> <!-- \\d+[KMG]? Suffixes are in terms of thousands -->")
Expand Down Expand Up @@ -479,7 +482,6 @@ func BenchmarkConsoleLog(b *testing.B) {
}
*/

stdout = ioutil.Discard
sl := NewDefaultLogger(INFO)
for i := 0; i < b.N; i++ {
sl.Log(WARNING, "here", "This is a log message")
Expand Down Expand Up @@ -510,7 +512,7 @@ func BenchmarkConsoleUtilNotLog(b *testing.B) {
func BenchmarkFileLog(b *testing.B) {
sl := make(Logger)
b.StopTimer()
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false))
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false, false))
b.StartTimer()
for i := 0; i < b.N; i++ {
sl.Log(WARNING, "here", "This is a log message")
Expand All @@ -522,7 +524,7 @@ func BenchmarkFileLog(b *testing.B) {
func BenchmarkFileNotLogged(b *testing.B) {
sl := make(Logger)
b.StopTimer()
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false))
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false, false))
b.StartTimer()
for i := 0; i < b.N; i++ {
sl.Log(DEBUG, "here", "This is a log message")
Expand All @@ -534,7 +536,7 @@ func BenchmarkFileNotLogged(b *testing.B) {
func BenchmarkFileUtilLog(b *testing.B) {
sl := make(Logger)
b.StopTimer()
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false))
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false, false))
b.StartTimer()
for i := 0; i < b.N; i++ {
sl.Info("%s is a log message", "This")
Expand All @@ -546,7 +548,7 @@ func BenchmarkFileUtilLog(b *testing.B) {
func BenchmarkFileUtilNotLog(b *testing.B) {
sl := make(Logger)
b.StopTimer()
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false))
sl.AddFilter("file", INFO, NewFileLogWriter("benchlog.log", false, false))
b.StartTimer()
for i := 0; i < b.N; i++ {
sl.Debug("%s is a log message", "This")
Expand Down
4 changes: 2 additions & 2 deletions socklog.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func NewSocketLogWriter(proto, hostport string) SocketLogWriter {
// Marshall into JSON
js, err := json.Marshal(rec)
if err != nil {
fmt.Fprint(os.Stderr, "SocketLogWriter(%q): %s", hostport, err)
fmt.Fprintf(os.Stderr, "SocketLogWriter(%q): %s\n", hostport, err)
return
}

_, err = sock.Write(js)
if err != nil {
fmt.Fprint(os.Stderr, "SocketLogWriter(%q): %s", hostport, err)
fmt.Fprintf(os.Stderr, "SocketLogWriter(%q): %s\n", hostport, err)
return
}
}
Expand Down
10 changes: 6 additions & 4 deletions termlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ import (
"time"
)

var stdout io.Writer = os.Stdout

// This is the standard writer that prints to standard output.
type ConsoleLogWriter struct {
format string
w chan *LogRecord
}

// This creates a new ConsoleLogWriter
func NewConsoleLogWriter() *ConsoleLogWriter {
func NewConsoleLogWriter(stderr bool) *ConsoleLogWriter {
consoleWriter := &ConsoleLogWriter{
format: "[%T %D] [%C] [%L] (%S) %M",
w: make(chan *LogRecord, LogBufferLength),
}
go consoleWriter.run(stdout)
out := os.Stdout
if stderr {
out = os.Stderr
}
go consoleWriter.run(out)
return consoleWriter
}
func (c *ConsoleLogWriter) SetFormat(format string) {
Expand Down
2 changes: 1 addition & 1 deletion xmlconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func xmlToConsoleLogWriter(filename string, props []xmlProperty, enabled bool) (
return nil, true
}

clw := NewConsoleLogWriter()
clw := NewConsoleLogWriter(false)
clw.SetFormat(format)

return clw, true
Expand Down