You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by oc...@apache.org on 2021/01/26 00:13:37 UTC

[trafficcontrol] branch master updated: Change lib/go-log to always log UTC (#5455)

This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 991fd83  Change lib/go-log to always log UTC (#5455)
991fd83 is described below

commit 991fd83d438ee1584781df0b87c0a80dd471aac7
Author: Robert O Butts <ro...@users.noreply.github.com>
AuthorDate: Mon Jan 25 17:13:23 2021 -0700

    Change lib/go-log to always log UTC (#5455)
    
    Without this, time zone will be arbitrary dependent on the OS.
---
 lib/go-log/log.go      |  4 ++--
 lib/go-log/log_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/lib/go-log/log.go b/lib/go-log/log.go
index 1470a2e..afb20e3 100644
--- a/lib/go-log/log.go
+++ b/lib/go-log/log.go
@@ -76,14 +76,14 @@ func logf(logger *log.Logger, format string, v ...interface{}) {
 	if logger == nil {
 		return
 	}
-	logger.Output(stackFrame, time.Now().Format(timeFormat)+": "+fmt.Sprintf(format, v...))
+	logger.Output(stackFrame, time.Now().UTC().Format(timeFormat)+": "+fmt.Sprintf(format, v...))
 }
 
 func logln(logger *log.Logger, v ...interface{}) {
 	if logger == nil {
 		return
 	}
-	logger.Output(stackFrame, time.Now().Format(timeFormat)+": "+fmt.Sprintln(v...))
+	logger.Output(stackFrame, time.Now().UTC().Format(timeFormat)+": "+fmt.Sprintln(v...))
 }
 
 const timeFormat = time.RFC3339Nano
diff --git a/lib/go-log/log_test.go b/lib/go-log/log_test.go
new file mode 100644
index 0000000..43dbf1f
--- /dev/null
+++ b/lib/go-log/log_test.go
@@ -0,0 +1,44 @@
+package log
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"bytes"
+	"io"
+	"strings"
+	"testing"
+)
+
+type writeCloser struct {
+	io.Writer
+}
+
+func (writeCloser) Close() error { return nil }
+
+func TestUTC(t *testing.T) {
+	buf := &bytes.Buffer{}
+	Init(nil, writeCloser{buf}, nil, nil, nil)
+	Errorln("test")
+	actual := buf.String()
+
+	if !strings.Contains(actual, "Z: test") {
+		t.Errorf("expected UTC time, actual '" + actual + "'")
+	}
+}