init.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2015 CoreOS, 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. //
  15. // +build !windows
  16. package capnslog
  17. import (
  18. "io"
  19. "os"
  20. "syscall"
  21. )
  22. // Here's where the opinionation comes in. We need some sensible defaults,
  23. // especially after taking over the log package. Your project (whatever it may
  24. // be) may see things differently. That's okay; there should be no defaults in
  25. // the main package that cannot be controlled or overridden programatically,
  26. // otherwise it's a bug. Doing so is creating your own init_log.go file much
  27. // like this one.
  28. func init() {
  29. initHijack()
  30. // Go `log` pacakge uses os.Stderr.
  31. SetFormatter(NewDefaultFormatter(os.Stderr))
  32. SetGlobalLogLevel(INFO)
  33. }
  34. func NewDefaultFormatter(out io.Writer) Formatter {
  35. if syscall.Getppid() == 1 {
  36. // We're running under init, which may be systemd.
  37. f, err := NewJournaldFormatter()
  38. if err == nil {
  39. return f
  40. }
  41. }
  42. return NewPrettyFormatter(out, false)
  43. }