1 // Go support for leveled logs, analogous to https://code.google.com/p/google-glog/
3 // Copyright 2013 Google Inc. All Rights Reserved.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
32 // MaxSize is the maximum size of a log file in bytes.
33 var MaxSize uint64 = 1024 * 1024 * 1800
35 // logDirs lists the candidate directories for new log files.
38 func createLogDirs() {
39 if logging.logDir != "" {
40 logDirs = append(logDirs, logging.logDir)
42 logDirs = append(logDirs, os.TempDir())
47 program = filepath.Base(os.Args[0])
49 userName = "unknownuser"
53 h, err := os.Hostname()
55 host = shortHostname(h)
58 current, err := user.Current()
60 userName = current.Username
63 // Sanitize userName since it may contain filepath separators on Windows.
64 userName = strings.Replace(userName, `\`, "_", -1)
67 // shortHostname returns its argument, truncating at the first period.
68 // For instance, given "www.google.com" it returns "www".
69 func shortHostname(hostname string) string {
70 if i := strings.Index(hostname, "."); i >= 0 {
76 // logName returns a new log file name containing tag, with start time t, and
77 // the name for the symlink for tag.
78 func logName(tag string, t time.Time) (name, link string) {
79 name = fmt.Sprintf("%s.%s.%s.log.%s.%04d%02d%02d-%02d%02d%02d.%d",
91 return name, program + "." + tag
94 var onceLogDirs sync.Once
96 // create creates a new log file and returns the file and its filename, which
97 // contains tag ("INFO", "FATAL", etc.) and t. If the file is created
98 // successfully, create also attempts to update the symlink for that tag, ignoring
100 // The startup argument indicates whether this is the initial startup of klog.
101 // If startup is true, existing files are opened for appending instead of truncated.
102 func create(tag string, t time.Time, startup bool) (f *os.File, filename string, err error) {
103 if logging.logFile != "" {
104 f, err := openOrCreate(logging.logFile, startup)
106 return f, logging.logFile, nil
108 return nil, "", fmt.Errorf("log: unable to create log: %v", err)
110 onceLogDirs.Do(createLogDirs)
111 if len(logDirs) == 0 {
112 return nil, "", errors.New("log: no log dirs")
114 name, link := logName(tag, t)
116 for _, dir := range logDirs {
117 fname := filepath.Join(dir, name)
118 f, err := openOrCreate(fname, startup)
120 symlink := filepath.Join(dir, link)
121 os.Remove(symlink) // ignore err
122 os.Symlink(name, symlink) // ignore err
127 return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr)
130 // The startup argument indicates whether this is the initial startup of klog.
131 // If startup is true, existing files are opened for appending instead of truncated.
132 func openOrCreate(name string, startup bool) (*os.File, error) {
134 f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
137 f, err := os.Create(name)