1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 18:51:28 +00:00

Merge pull request #1843 from JessonChan/beelog_bug_fix

Beelog bug fix
This commit is contained in:
astaxie 2016-03-30 15:44:20 +08:00
commit 8ce9f69b4d
3 changed files with 89 additions and 29 deletions

View File

@ -255,7 +255,7 @@ func (bl *BeeLogger) Write(p []byte) (n int, err error) {
} }
func (bl *BeeLogger) writeMsg(logLevel int, msg string, v ...interface{}) error { func (bl *BeeLogger) writeMsg(logLevel int, msg string, v ...interface{}) error {
if !beeLogger.init { if !bl.init {
bl.lock.Lock() bl.lock.Lock()
bl.setLogger(AdapterConsole) bl.setLogger(AdapterConsole)
bl.lock.Unlock() bl.lock.Unlock()

View File

@ -36,43 +36,46 @@ func (lg *logWriter) println(when time.Time, msg string) {
lg.Unlock() lg.Unlock()
} }
const y1 = `0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999`
const y2 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789`
const mo1 = `000000000111`
const mo2 = `123456789012`
const d1 = `0000000001111111111222222222233`
const d2 = `1234567890123456789012345678901`
const h1 = `000000000011111111112222`
const h2 = `012345678901234567890123`
const mi1 = `000000000011111111112222222222333333333344444444445555555555`
const mi2 = `012345678901234567890123456789012345678901234567890123456789`
const s1 = `000000000011111111112222222222333333333344444444445555555555`
const s2 = `012345678901234567890123456789012345678901234567890123456789`
func formatTimeHeader(when time.Time) ([]byte, int) { func formatTimeHeader(when time.Time) ([]byte, int) {
y, mo, d := when.Date() y, mo, d := when.Date()
h, mi, s := when.Clock() h, mi, s := when.Clock()
//len(2006/01/02 15:03:04)==19 //len("2006/01/02 15:04:05 ")==20
var buf [20]byte var buf [20]byte
t := 3
for y >= 10 { //change to '3' after 984 years, LOL
p := y / 10 buf[0] = '2'
buf[t] = byte('0' + y - p*10) //change to '1' after 84 years, LOL
y = p buf[1] = '0'
t-- buf[2] = y1[y-2000]
} buf[3] = y2[y-2000]
buf[0] = byte('0' + y)
buf[4] = '/' buf[4] = '/'
if mo > 9 { buf[5] = mo1[mo-1]
buf[5] = '1' buf[6] = mo2[mo-1]
buf[6] = byte('0' + mo - 9)
} else {
buf[5] = '0'
buf[6] = byte('0' + mo)
}
buf[7] = '/' buf[7] = '/'
t = d / 10 buf[8] = d1[d-1]
buf[8] = byte('0' + t) buf[9] = d2[d-1]
buf[9] = byte('0' + d - t*10)
buf[10] = ' ' buf[10] = ' '
t = h / 10 buf[11] = h1[h]
buf[11] = byte('0' + t) buf[12] = h2[h]
buf[12] = byte('0' + h - t*10)
buf[13] = ':' buf[13] = ':'
t = mi / 10 buf[14] = mi1[mi]
buf[14] = byte('0' + t) buf[15] = mi2[mi]
buf[15] = byte('0' + mi - t*10)
buf[16] = ':' buf[16] = ':'
t = s / 10 buf[17] = s1[s]
buf[17] = byte('0' + t) buf[18] = s2[s]
buf[18] = byte('0' + s - t*10)
buf[19] = ' ' buf[19] = ' '
return buf[0:], d return buf[0:], d

57
logs/logger_test.go Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2016 beego Author. All Rights Reserved.
//
// Licensed 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.
package logs
import (
"testing"
"time"
)
func TestFormatHeader_0(t *testing.T) {
tm := time.Now()
if tm.Year() >= 2100 {
t.FailNow()
}
dur := time.Second
for {
if tm.Year() >= 2100 {
break
}
h, _ := formatTimeHeader(tm)
if tm.Format("2006/01/02 15:04:05 ") != string(h) {
t.Log(tm)
t.FailNow()
}
tm = tm.Add(dur)
dur *= 2
}
}
func TestFormatHeader_1(t *testing.T) {
tm := time.Now()
year := tm.Year()
dur := time.Second
for {
if tm.Year() >= year+1 {
break
}
h, _ := formatTimeHeader(tm)
if tm.Format("2006/01/02 15:04:05 ") != string(h) {
t.Log(tm)
t.FailNow()
}
tm = tm.Add(dur)
}
}