github.com/astaxie/beego@v1.12.3/logs/console_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     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  package logs
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  // Try each log level in decreasing order of priority.
    23  func testConsoleCalls(bl *BeeLogger) {
    24  	bl.Emergency("emergency")
    25  	bl.Alert("alert")
    26  	bl.Critical("critical")
    27  	bl.Error("error")
    28  	bl.Warning("warning")
    29  	bl.Notice("notice")
    30  	bl.Informational("informational")
    31  	bl.Debug("debug")
    32  }
    33  
    34  // Test console logging by visually comparing the lines being output with and
    35  // without a log level specification.
    36  func TestConsole(t *testing.T) {
    37  	log1 := NewLogger(10000)
    38  	log1.EnableFuncCallDepth(true)
    39  	log1.SetLogger("console", "")
    40  	testConsoleCalls(log1)
    41  
    42  	log2 := NewLogger(100)
    43  	log2.SetLogger("console", `{"level":3}`)
    44  	testConsoleCalls(log2)
    45  }
    46  
    47  // Test console without color
    48  func TestConsoleNoColor(t *testing.T) {
    49  	log := NewLogger(100)
    50  	log.SetLogger("console", `{"color":false}`)
    51  	testConsoleCalls(log)
    52  }
    53  
    54  // Test console async
    55  func TestConsoleAsync(t *testing.T) {
    56  	log := NewLogger(100)
    57  	log.SetLogger("console")
    58  	log.Async()
    59  	//log.Close()
    60  	testConsoleCalls(log)
    61  	for len(log.msgChan) != 0 {
    62  		time.Sleep(1 * time.Millisecond)
    63  	}
    64  }