Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add source field to log record

  • Loading branch information...
commit 848d104bd2efb2f2274bbaa7ef7a7648978e21a7 1 parent 88554d0
@pietern pietern authored
View
2  io_sink_test.go
@@ -19,7 +19,7 @@ func (s *IOSinkSuite) TestAddRecord(c *C) {
sink.SetCodec(NewJsonCodec())
go func(msg string) {
- record := NewRecord(LOG_INFO, msg, map[string]string{})
+ record := NewRecord("source", LOG_INFO, msg, map[string]string{})
sink.AddRecord(record)
sink.Flush()
pWriter.Close()
View
2  json_codec_test.go
@@ -10,7 +10,7 @@ type JsonCodecSuite struct {
var _ = Suite(&JsonCodecSuite{})
func (s *JsonCodecSuite) TestJsonCodec(c *C) {
- record := NewRecord(LOG_INFO, "Hello world", map[string]string{})
+ record := NewRecord("source", LOG_INFO, "Hello world", map[string]string{})
codec := NewJsonCodec()
msg, _ := codec.EncodeRecord(record)
c.Assert(string(msg), Matches, `{.*"Message":"Hello world".*}`)
View
8 json_prettifier_test.go
@@ -18,7 +18,7 @@ func (s *JsonPrettifierSuite) TestConst(c *C) {
}
func (s *JsonPrettifierSuite) TestConstOrder(c *C) {
- record := NewRecord(LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
prettifier1 := NewJsonPrettifier(EXCLUDE_FILE | EXCLUDE_DATA)
bytes1, _ := prettifier1.EncodeRecord(record)
@@ -31,7 +31,7 @@ func (s *JsonPrettifierSuite) TestConstOrder(c *C) {
func (s *JsonPrettifierSuite) TestEncodeRecord(c *C) {
config.EnableLOC = true
- record := NewRecord(LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
config.EnableLOC = false
l := record.Line
@@ -46,7 +46,7 @@ func (s *JsonPrettifierSuite) TestEncodeRecord(c *C) {
func (s *JsonPrettifierSuite) TestExclude(c *C) {
config.EnableLOC = true
- record := NewRecord(LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
config.EnableLOC = false
prettifier := NewJsonPrettifier(EXCLUDE_DATA | EXCLUDE_LINE)
@@ -59,7 +59,7 @@ func (s *JsonPrettifierSuite) TestExclude(c *C) {
func (s *JsonPrettifierSuite) TestDecodeLogEntry(c *C) {
config.EnableLOC = true
- record := NewRecord(LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
+ record := NewRecord("source", LOG_INFO, "Hello, world", map[string]string{"foo": "bar"})
config.EnableLOC = false
l := record.Line
t := record.Timestamp
View
2  logger.go
@@ -38,7 +38,7 @@ func (l *BaseLogger) Log(level LogLevel, message string, data map[string]string)
return
}
- record := NewRecord(level, message, data)
+ record := NewRecord(l.name, level, message, data)
for _, sink := range l.sinks {
sink.AddRecord(record)
View
28 record.go
@@ -9,21 +9,23 @@ import (
// FIXME: Missing fields
type Record struct {
Timestamp float64
- Message string
+ Source string
Level LogLevel
+ Message string
Data map[string]string
File string
- Method string
Line int
+ Method string
}
-func NewRecord(level LogLevel, message string, data map[string]string) *Record {
- record := new(Record)
-
- record.Timestamp = float64(time.Now().UnixNano()) / 1000000000
- record.Message = message
- record.Level = level
- record.Data = data
+func NewRecord(s string, l LogLevel, m string, d map[string]string) *Record {
+ r := &Record{
+ Timestamp: float64(time.Now().UnixNano()) / 1000000000,
+ Source: s,
+ Level: l,
+ Message: m,
+ Data: d,
+ }
if config.EnableLOC {
var function *runtime.Func
@@ -39,10 +41,10 @@ func NewRecord(level LogLevel, message string, data map[string]string) *Record {
break
}
}
- record.File = file
- record.Method = function.Name()
- record.Line = line
+ r.File = file
+ r.Line = line
+ r.Method = function.Name()
}
- return record
+ return r
}
View
25 record_test.go
@@ -4,27 +4,24 @@ import (
. "launchpad.net/gocheck"
)
-type RecordSuite struct {
-}
+type RecordSuite struct{}
var _ = Suite(&RecordSuite{})
-func (s *RecordSuite) TestNewRecordWithLOCEnabled(c *C) {
- message := "Hello, GOSTENO"
+func (s *RecordSuite) TestNewRecordWithLOC(c *C) {
config.EnableLOC = true
- record := NewRecord(LOG_INFO, message, map[string]string{}) // Line 15
+ r := NewRecord("source", LOG_INFO, "hello", map[string]string{})
config.EnableLOC = false
- c.Assert(record.Line, Equals, 15)
- c.Assert(record.Method, Matches, `.*\.\(\*RecordSuite\)\.TestNewRecordWithLOCEnabled`)
- c.Assert(record.File, Matches, ".*record_test.go$")
+ c.Check(r.File, Matches, ".*record_test.go$")
+ c.Check(r.Line, Equals, 13)
+ c.Check(r.Method, Matches, `.*\.\(\*RecordSuite\)\.TestNewRecordWithLOC`)
}
-func (s *RecordSuite) TestNewRecordWithLOCDisabled(c *C) {
- message := "Hello, world"
- record := NewRecord(LOG_INFO, message, map[string]string{})
+func (s *RecordSuite) TestNewRecordWithoutLOC(c *C) {
+ r := NewRecord("source", LOG_INFO, "hello", map[string]string{})
- c.Assert(record.Line, Equals, 0)
- c.Assert(record.Method, Equals, "")
- c.Assert(record.File, Equals, "")
+ c.Check(r.File, Equals, "")
+ c.Check(r.Line, Equals, 0)
+ c.Check(r.Method, Equals, "")
}
Please sign in to comment.
Something went wrong with that request. Please try again.