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