github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/mungers/publisher_logger.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Changing glog output directory via --log_dir doesn't work, because the flag
    18  // is parsed after the first invocation of glog, so the log file ends up in the
    19  // temporary directory. Hence, we manually duplicates glog ouptut.
    20  
    21  package mungers
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"time"
    27  
    28  	"github.com/golang/glog"
    29  )
    30  
    31  type plog struct {
    32  	buf *bytes.Buffer
    33  }
    34  
    35  func NewPublisherLog(buf *bytes.Buffer) *plog {
    36  	return &plog{buf}
    37  }
    38  
    39  func (p *plog) write(format string, args ...interface{}) {
    40  	p.buf.WriteString("[" + time.Now().Format(time.RFC822) + "]: ")
    41  	p.buf.WriteString(fmt.Sprintf(format, args...))
    42  	p.buf.WriteString("\n")
    43  }
    44  
    45  func (p *plog) Errorf(format string, args ...interface{}) {
    46  	glog.ErrorDepth(1, fmt.Sprintf(format, args...))
    47  	p.write(format, args...)
    48  }
    49  
    50  func (p *plog) Infof(format string, args ...interface{}) {
    51  	glog.InfoDepth(1, fmt.Sprintf(format, args...))
    52  	p.write(format, args...)
    53  }
    54  
    55  func (p *plog) Fatalf(format string, args ...interface{}) {
    56  	glog.FatalDepth(1, fmt.Sprintf(format, args...))
    57  	p.write(format, args...)
    58  }
    59  
    60  func (p *plog) ReadLog() string {
    61  	return p.buf.String()
    62  }
    63  
    64  func (p *plog) Flush() {
    65  	glog.Flush()
    66  }