handler.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (C) 2014 Space Monkey, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spacelog
  15. import (
  16. "text/template"
  17. )
  18. // Handler is an interface that knows how to process log events. This is the
  19. // basic interface type for building a logging system. If you want to route
  20. // structured log data somewhere, you would implement this interface.
  21. type Handler interface {
  22. // Log is called for every message. if calldepth is negative, caller
  23. // information is missing
  24. Log(logger_name string, level LogLevel, msg string, calldepth int)
  25. // These two calls are expected to be no-ops on non-text-output handlers
  26. SetTextTemplate(t *template.Template)
  27. SetTextOutput(output TextOutput)
  28. }
  29. // HandlerFunc is a type to make implementation of the Handler interface easier
  30. type HandlerFunc func(logger_name string, level LogLevel, msg string,
  31. calldepth int)
  32. // Log simply calls f(logger_name, level, msg, calldepth)
  33. func (f HandlerFunc) Log(logger_name string, level LogLevel, msg string,
  34. calldepth int) {
  35. f(logger_name, level, msg, calldepth)
  36. }
  37. // SetTextTemplate is a no-op
  38. func (HandlerFunc) SetTextTemplate(t *template.Template) {}
  39. // SetTextOutput is a no-op
  40. func (HandlerFunc) SetTextOutput(output TextOutput) {}
  41. var (
  42. defaultHandler = NewTextHandler(StdlibTemplate,
  43. &StdlibOutput{})
  44. )