func (s *ManagerTestSuite) TestSend() { m := gol.NewEmergency("field", "value") // l1 will not filter the message mf1 := &mfmock.LogFilter{} mf1.On("Filter", m).Return(false) mfmt1 := &mfmock.LogFormatter{} mfmt1.On("Format", m).Return("EMERGENCY field=value", nil) mw1 := &mfmock.Writer{} mw1.On("Write", mock.Anything).Return(21, nil) l1 := logger_simple.New(mf1, mfmt1, mw1) // l2 will filter the message mf2 := &mfmock.LogFilter{} mf2.On("Filter", m).Return(true) mfmt2 := &mfmock.LogFormatter{} mw2 := &mfmock.Writer{} l2 := logger_simple.New(mf2, mfmt2, mw2) s.manager.Register("l1", l1) s.manager.Register("l2", l2) s.manager.Run() assert.Nil(s.T(), s.manager.Send(m)) time.Sleep(1 * time.Second) s.manager.Close() mf1.AssertExpectations(s.T()) mfmt1.AssertExpectations(s.T()) mw1.AssertExpectations(s.T()) mf2.AssertExpectations(s.T()) mfmt2.AssertExpectations(s.T()) mw2.AssertExpectations(s.T()) }
func (s *LoggerTestSuite) TestSend() { in := map[string]setupLogTest{ "error": setupLogTest{ setUp: func( msg *gol.LogMessage, mf *mock.LogFilter, mfmt *mock.LogFormatter, mw *mock.Writer, ) (logger *simple.Logger) { mf.Mock.On("Filter", msg).Return(false, nil) mfmt.Mock.On("Format", msg).Return("ERROR", nil) mw.Mock.On("Write", []byte("ERROR")).Return(5, nil) logger = simple.New(mf, mfmt, mw) return }, message: gol.NewError(), output: "ERROR", }, "info": setupLogTest{ setUp: func( msg *gol.LogMessage, mf *mock.LogFilter, mfmt *mock.LogFormatter, mw *mock.Writer, ) (logger *simple.Logger) { mf.Mock.On("Filter", msg).Return(true, nil) logger = simple.New(mf, mfmt, mw) return }, message: gol.NewInfo(), output: "", }, } for _, t := range in { mf := &mock.LogFilter{} mfmt := &mock.LogFormatter{} mw := &mock.Writer{} logger := t.setUp(t.message, mf, mfmt, mw) logger.Send(t.message) mf.AssertExpectations(s.T()) mfmt.AssertExpectations(s.T()) mw.AssertExpectations(s.T()) } }
func (s *LoggerTestSuite) TestSendNilMessage() { mf := &mock.LogFilter{} mfmt := &mock.LogFormatter{} mw := &mock.Writer{} logger := simple.New(mf, mfmt, mw) assert.Nil(s.T(), logger.Send(nil)) }
func (s *LoggerTestSuite) TestSendNilFormatter() { msg := gol.NewDebug() mf := &mock.LogFilter{} mf.Mock.On("Filter", msg).Return(false, nil) logger := simple.New(mf, nil, nil) assert.Error(s.T(), logger.Send(msg)) }
func (s *LoggerTestSuite) TestGetSetWriter() { l := simple.New(nil, nil, nil) assert.Nil(s.T(), l.Writer()) assert.Nil(s.T(), l.SetWriter(&mock.Writer{})) assert.NotNil(s.T(), l.Writer()) assert.Error(s.T(), l.SetWriter(nil)) }
func (s *LoggerTestSuite) TestGetSetFilter() { l := simple.New(nil, nil, nil) assert.Nil(s.T(), l.Filter()) assert.Nil(s.T(), l.SetFilter(&mock.LogFilter{})) assert.NotNil(s.T(), l.Filter()) assert.Nil(s.T(), l.SetFilter(nil)) }
func init() { file, err := os.OpenFile("info.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { os.Stderr.WriteString(fmt.Sprintf("ERROR %s\n", err)) } multi := io.MultiWriter(file, os.Stdout) txtFmt := &formatters.Text{} log = simple.New(nil, txtFmt, multi) }
func (s *LoggerTestSuite) TestSendFormatError() { msg := gol.NewDebug() mf := &mock.LogFilter{} mf.Mock.On("Filter", msg).Return(false, nil) mfmt := &mock.LogFormatter{} mfmt.Mock.On("Format", msg).Return("", fmt.Errorf("unknown")) logger := simple.New(mf, mfmt, nil) assert.Error(s.T(), logger.Send(msg)) }
func init() { fmt.Println("init():start") Log = manager_simple.New(LogWorkers) f := filter_severity.New(field_severity.Info) formatter := formatters.Text{} logger := logger_simple.New(f, formatter, os.Stdout) Log.Register("main", logger) Log.Run() Log.Send(gol.NewInfo("message", "main.Log has been configured")) fmt.Println("init():end") }
if severity, err := msg.Severity(); err != nil { return fmt.Sprintf("%s UNKNOWN %s\n", t.String(), strings.Join(buffer, " ")), nil } else { switch severity >= field_severity.Error { case true: return fmt.Sprintf("%s %s %s\n", t.String(), color.RedString("%s", severity), strings.Join(buffer, " ")), nil default: return fmt.Sprintf("%s %s %s\n", t.String(), severity, strings.Join(buffer, " ")), nil } } } var _ gol.LogFormatter = (*Custom)(nil) var logger gol.Logger = simple.New(nil, &Custom{}, os.Stdout) func main() { // this will be written to stderr logger.Send(gol.NewEmergency("message", "system is down")) logger.Send(gol.NewAlert("message", "failed to write to disk")) logger.Send(gol.NewCritical("message", "high server load")) logger.Send(gol.NewError("message", "invalid number format")) // this will not be written anywhere logger.Send(gol.NewWarning("message", "performance close to 1s threshold")) logger.Send(gol.NewNotice("message", "failed to communicate with monitoring service")) logger.Send(gol.NewInfo("message", "requested processed in 250ms")) logger.Send(gol.NewDebug("debug", "var x = 10")) }
// package main import ( "os" "github.com/mediaFORGE/gol" field "github.com/mediaFORGE/gol/fields/severity" filter "github.com/mediaFORGE/gol/filters/severity" "github.com/mediaFORGE/gol/formatters" "github.com/mediaFORGE/gol/loggers/simple" ) var txtFmt = &formatters.Text{} var errorLog gol.Logger = simple.New(filter.New(field.Error), txtFmt, os.Stderr) func main() { // this will be written to stderr errorLog.Send(gol.NewEmergency("message", "system is down")) errorLog.Send(gol.NewAlert("message", "failed to write to disk")) errorLog.Send(gol.NewCritical("message", "high server load")) errorLog.Send(gol.NewError("message", "invalid number format")) // this will not be written anywhere errorLog.Send(gol.NewWarning("message", "performance close to 1s threshold")) errorLog.Send(gol.NewNotice("message", "failed to communicate with monitoring service")) errorLog.Send(gol.NewInfo("message", "requested processed in 250ms")) errorLog.Send(gol.NewDebug("debug", "var x = 10")) }
// New creates and initializes a mock logger struct. func New() gol.Logger { return simple.New(&mock.LogFilter{}, &mock.LogFormatter{}, nil) }
type InfoFilter struct{} func (f *InfoFilter) Filter(msg *gol.LogMessage) bool { if s, err := msg.Severity(); err != nil { // no severity return true } else { return s < severity.Warning } } var _ gol.LogFilter = (*InfoFilter)(nil) var txtFmt = &formatters.Text{} var log gol.Logger = simple.New(&InfoFilter{}, txtFmt, os.Stdout) var errorLog gol.Logger = simple.New(&ErrorFilter{}, txtFmt, os.Stderr) func main() { log.Send(&gol.LogMessage{"hello": "world!"}) log.Send(gol.NewError("error", "info")) log.Send(gol.NewDebug("debug", "info")) errorLog.Send(&gol.LogMessage{"hello": "world!"}) errorLog.Send(gol.NewEmergency("emergency", "errorLog")) errorLog.Send(gol.NewError("error", "errorLog")) errorLog.Send(gol.NewWarning("error", "errorLog")) errorLog.Send(gol.NewInfo("info", "errorLog")) errorLog.Send(gol.NewDebug("debug", "errorLog")) }
// You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // package main import ( "os" "github.com/mediaFORGE/gol" "github.com/mediaFORGE/gol/formatters" "github.com/mediaFORGE/gol/loggers/simple" ) var txtFmt = &formatters.Text{} var log gol.Logger = simple.New(nil, txtFmt, os.Stdout) var errorLog gol.Logger = simple.New(nil, txtFmt, os.Stderr) func main() { log.Send(&gol.LogMessage{"message": "written to log"}) errorLog.Send(&gol.LogMessage{"message": "written to error log"}) }