Exemplo n.º 1
0
func TestStdLoggerFormat(t *testing.T) {

	var buf bytes.Buffer
	var bufInfo bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelDebug),
		log.SetStdWriter(&buf),
		log.SetStdInfo(&bufInfo, "TEST-INFO ", std.LstdFlags),
	)

	sl.Debug("my Debug", 3.14152)
	sl.Debug("my Debug2", "", 2.14152)
	sl.Debug("my Debug3", "key3", 3105, 4711, "Hello")
	sl.Info("InfoTEST")
	sl.Info("InfoTEST", "keyI", 117, 2009)
	sl.Info("InfoTEST", "Now we have the salad")

	logs := buf.String()
	logsInfo := bufInfo.String()

	assert.Contains(t, logs, "Debug2")
	assert.Contains(t, logs, "BAD_KEY_AT_INDEX_0")
	assert.Contains(t, logs, `key3: 3105 BAD_KEY_AT_INDEX_2: "Hello"`)

	assert.Contains(t, logsInfo, "InfoTEST")
	assert.Contains(t, logsInfo, `_: "Now we have the salad`)
	assert.Contains(t, logsInfo, `FIX_IMBALANCED_PAIRS: []interface {}{"keyI", 117, 2009}`)
}
Exemplo n.º 2
0
func TestStdLoggerFormat(t *testing.T) {

	var buf bytes.Buffer
	var bufInfo bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelDebug),
		log.SetStdWriter(&buf),
		log.SetStdInfo(&bufInfo, "TEST-INFO ", std.LstdFlags),
	)

	sl.Debug("my Debug", 3.14152)
	sl.Debug("my Debug2", "", 2.14152)
	sl.Debug("my Debug3", "key3", 3105, 4711, "Hello")
	sl.Info("InfoTEST")
	sl.Info("InfoTEST", "keyI", 117, 2009)

	aTestErr := errors.New("Cannot run PHP code")
	haveErr := sl.Error("ErrorTEST", "myErr", aTestErr)

	logs := buf.String()
	logsInfo := bufInfo.String()

	//	t.Log("", logs)
	//	t.Log("", logsInfo)

	assert.EqualError(t, haveErr, aTestErr.Error())
	assert.Contains(t, logs, "Debug2")
	assert.Contains(t, logs, "BAD_KEY_AT_INDEX_0")
	assert.Contains(t, logs, `key3: 3105 BAD_KEY_AT_INDEX_2: "Hello"`)
	assert.Contains(t, logs, "_: 3.14")

	assert.Contains(t, logsInfo, "InfoTEST")
	assert.Contains(t, logsInfo, `FIX_IMBALANCED_PAIRS: []interface {}{"keyI", 117, 2009}`)
}
Exemplo n.º 3
0
func TestWithAccessLog(t *testing.T) {
	var buf bytes.Buffer
	defer buf.Reset()

	ctx := ctxlog.NewContext(context.Background(), log.NewStdLogger(log.SetStdWriter(&buf)))

	finalH := ctxhttp.Chain(
		ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
			w.WriteHeader(http.StatusTeapot)
			_, err := w.Write([]byte{'1', '2', '3'})
			time.Sleep(time.Millisecond)
			return err
		}),
		ctxhttp.WithAccessLog(),
	)

	r, _ := http.NewRequest("GET", "/gopherine", nil)
	r.RemoteAddr = "127.0.0.1"
	r.Header.Set("User-Agent", "Mozilla")
	r.Header.Set("Referer", "http://rustlang.org")

	w := httptest.NewRecorder()
	if err := finalH.ServeHTTPContext(ctx, w, r); err != nil {
		t.Fatal(err)
	}

	assert.Exactly(t, `123`, w.Body.String())
	assert.Exactly(t, http.StatusTeapot, w.Code)

	want1 := `request error: "" method: "GET" uri: "/gopherine" type: "access" status: "error" status_code: 418 duration:`
	want2 := `size: 3 remote_addr: "127.0.0.1" user_agent: "Mozilla" referer: "http://rustlang.org"`
	assert.Contains(t, buf.String(), want1)
	assert.Contains(t, buf.String(), want2)
}
Exemplo n.º 4
0
func TestDaemonOfflineLogger(t *testing.T) {
	offLog := mail.OfflineLogger
	defer func() {
		mail.OfflineLogger = offLog
	}()

	var logBufI bytes.Buffer
	var logBufE bytes.Buffer
	mail.OfflineLogger = log.NewStdLogger(
		log.SetStdLevel(log.StdLevelInfo),
		log.SetStdInfo(&logBufI, "test", std.LstdFlags),
		log.SetStdError(&logBufE, "test", std.LstdFlags),
	)

	dm, err := mail.NewDaemon()
	dm.Config = configMock
	dm.ScopeID = config.ScopeID(3001)

	assert.NoError(t, err)
	assert.NotNil(t, dm)
	assert.True(t, dm.IsOffline())

	go func() { assert.NoError(t, dm.Worker()) }()
	assert.NoError(t, dm.SendPlain("gopher@earth", "apple@mothership", "Phoning home", "Hey Apple stop phoning home or you become apple puree"))
	assert.NoError(t, dm.Stop())
	assert.True(t, mail.OfflineLogger.IsInfo())

	time.Sleep(time.Millisecond) // waiting for channel to drain

	assert.Contains(t, logBufI.String(), `Send from: "gopher@earth" to: []string{"apple@mothership"} msg: "Mime-Version: 1.0`)
	assert.Empty(t, logBufE.String())

}
Exemplo n.º 5
0
func TestStdLogger(t *testing.T) {

	var buf bytes.Buffer

	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelTrace),
		log.SetStdTrace(&buf, "TEST-TRACE ", std.LstdFlags),
		log.SetStdDebug(&buf, "TEST-DEBUG ", std.LstdFlags),
		log.SetStdInfo(&buf, "TEST-INFO ", std.LstdFlags),
		log.SetStdWarn(&buf, "TEST-WARN ", std.LstdFlags),
		log.SetStdError(&buf, "TEST-ERROR ", std.LstdFlags),
		log.SetStdFatal(&buf, "TEST-FATAL ", std.LstdFlags),
	)
	sl.SetLevel(log.StdLevelInfo)
	assert.False(t, sl.IsTrace())
	assert.False(t, sl.IsDebug())
	assert.True(t, sl.IsInfo())
	assert.True(t, sl.IsWarn())

	sl.Trace("my trace1")
	sl.Trace("my trace2", "int", 29)
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")
	sl.Warn("WarnTEST")
	haveErr := sl.Error("ErrorTEST", "err1a", 1, "err2", 32.4232)
	assert.Contains(t, "ErrorTEST53", sl.Error("ErrorTEST53").Error())
	logs := buf.String()

	assert.EqualError(t, haveErr, "ErrorTEST")
	assert.Contains(t, logs, "InfoTEST")
	assert.Contains(t, logs, "WarnTEST")
	assert.Contains(t, logs, "ErrorTEST")
	assert.NotContains(t, logs, "trace1")
	assert.NotContains(t, logs, "Debug2")

	buf.Reset()
	sl.SetLevel(log.StdLevelTrace)
	assert.True(t, sl.IsTrace())
	assert.True(t, sl.IsDebug())
	assert.True(t, sl.IsInfo())
	assert.True(t, sl.IsWarn())
	sl.Trace("my trace1")
	sl.Trace("my trace2", "int", 29)
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")

	logs = buf.String()

	assert.Contains(t, logs, "InfoTEST")
	assert.Contains(t, logs, "trace1")
	assert.Contains(t, logs, "Debug2")

}
Exemplo n.º 6
0
func TestContext(t *testing.T) {
	l := log.NewStdLogger()
	ctx := context.Background()
	ctx = ctxlog.NewContext(ctx, l)

	haveL := ctxlog.FromContext(ctx)
	assert.Exactly(t, l, haveL)

	haveL2 := ctxlog.FromContext(context.TODO())
	assert.Exactly(t, log.BlackHole{}, haveL2)
}
Exemplo n.º 7
0
func TestStdLoggerFatal(t *testing.T) {

	defer func() {
		if r := recover(); r != nil {
			assert.Contains(t, r.(string), "This is sparta")
		}
	}()

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdWriter(&buf),
	)
	sl.Fatal("This is sparta")
}
Exemplo n.º 8
0
func TestStdLoggerNewPanic(t *testing.T) {

	defer func() {
		if r := recover(); r != nil {
			if msg, ok := r.(string); ok {
				assert.EqualValues(t, "Arguments to New() can only be StdOption types!", msg)
			} else {
				t.Error("Expecting a string")
			}
		}
	}()

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdWriter(&buf),
	)
	sl.New(log.SetStdLevel(log.StdLevelDebug), 1)
}
Exemplo n.º 9
0
func TestStdLoggerGlobals(t *testing.T) {

	var buf bytes.Buffer
	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelDebug),
		log.SetStdWriter(&buf),
		log.SetStdFlag(std.Ldate),
	)
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")

	logs := buf.String()

	assert.NotContains(t, logs, "trace2")
	assert.Contains(t, logs, "InfoTEST")
	assert.NotContains(t, logs, "trace1")
	assert.Contains(t, logs, "Debug2")
}
Exemplo n.º 10
0
func TestStdLogger(t *testing.T) {

	var buf bytes.Buffer

	sl := log.NewStdLogger(
		log.SetStdLevel(log.StdLevelDebug),
		log.SetStdDebug(&buf, "TEST-DEBUG ", std.LstdFlags),
		log.SetStdInfo(&buf, "TEST-INFO ", std.LstdFlags),
		log.SetStdFatal(&buf, "TEST-FATAL ", std.LstdFlags),
	)
	sl.SetLevel(log.StdLevelInfo)
	assert.False(t, sl.IsDebug())
	assert.True(t, sl.IsInfo())

	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")

	logs := buf.String()

	assert.Contains(t, logs, "InfoTEST")
	assert.NotContains(t, logs, "Debug2")

	buf.Reset()
	sl.SetLevel(log.StdLevelDebug)
	assert.True(t, sl.IsDebug())
	assert.True(t, sl.IsInfo())
	sl.Debug("my Debug", "float", 3.14152)
	sl.Debug("my Debug2", 2.14152)
	sl.Info("InfoTEST")

	logs = buf.String()

	assert.Contains(t, logs, "InfoTEST")
	assert.Contains(t, logs, "Debug2")

}
func initStore() {
	store.PkgLog = log.NewStdLogger(
		log.SetStdDebug(&testDebugLogBuf, "testErr: ", std.Lshortfile),
	)
	store.PkgLog.SetLevel(log.StdLevelDebug)

	testStoreService = store.MustNewService(
		scope.Option{
			Website: scope.MockID(1), // bound to website ID 1 = Europe
		},
		store.NewStorage(
			// Storage gets usually loaded from the database tables containing
			// website, group and store. For the sake of this example the storage
			// is hard coded.
			store.SetStorageWebsites(
				&store.TableWebsite{WebsiteID: 0, Code: dbr.NewNullString("admin"), Name: dbr.NewNullString("Admin"), SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NewNullBool(false)},
				&store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)},
				&store.TableWebsite{WebsiteID: 2, Code: dbr.NewNullString("oz"), Name: dbr.NewNullString("OZ"), SortOrder: 20, DefaultGroupID: 3, IsDefault: dbr.NewNullBool(false)},
			),
			store.SetStorageGroups(
				&store.TableGroup{GroupID: 3, WebsiteID: 2, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 5},
				&store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2},
				&store.TableGroup{GroupID: 0, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0},
				&store.TableGroup{GroupID: 2, WebsiteID: 1, Name: "UK Group", RootCategoryID: 2, DefaultStoreID: 4},
			),
			store.SetStorageStores(
				&store.TableStore{StoreID: 0, Code: dbr.NewNullString("admin"), WebsiteID: 0, GroupID: 0, Name: "Admin", SortOrder: 0, IsActive: true},
				&store.TableStore{StoreID: 5, Code: dbr.NewNullString("au"), WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, IsActive: true},
				&store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true},
				&store.TableStore{StoreID: 4, Code: dbr.NewNullString("uk"), WebsiteID: 1, GroupID: 2, Name: "UK", SortOrder: 10, IsActive: true},
				&store.TableStore{StoreID: 2, Code: dbr.NewNullString("at"), WebsiteID: 1, GroupID: 1, Name: "Österreich", SortOrder: 20, IsActive: true},
				&store.TableStore{StoreID: 6, Code: dbr.NewNullString("nz"), WebsiteID: 2, GroupID: 3, Name: "Kiwi", SortOrder: 30, IsActive: true},
				&store.TableStore{IsActive: false, StoreID: 3, Code: dbr.NewNullString("ch"), WebsiteID: 1, GroupID: 1, Name: "Schweiz", SortOrder: 30},
			),
		),
	)
}
Exemplo n.º 12
0
func init() {
	log.Set(log.NewStdLogger(
		log.SetStdError(&errLogBuf, "testErr", std.LstdFlags),
	))
}
Exemplo n.º 13
0
func init() {
	log.Set(log.NewStdLogger())
	log.SetLevel(log.StdLevelDebug)
}
Exemplo n.º 14
0
Arquivo: log.go Projeto: levcom/csfw
// Copyright 2015, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// 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 codegen

import "github.com/corestoreio/csfw/utils/log"

// PkgLog global package based logger
var PkgLog log.Logger = log.NewStdLogger()
Exemplo n.º 15
0
func init() {
	log.Set(log.NewStdLogger(
		log.SetStdError(&errLogBuf, "testErr: ", std.Lshortfile),
	))
	log.SetLevel(log.StdLevelError)
}
Exemplo n.º 16
0
func init() {
	log.Set(log.NewStdLogger())
	log.SetLevel(log.StdLevelError)
}
Exemplo n.º 17
0
func init() {
	config.PkgLog = log.NewStdLogger(
		log.SetStdDebug(&errLogBuf, "testErr: ", std.Lshortfile),
	)
	config.PkgLog.SetLevel(log.StdLevelDebug)
}
Exemplo n.º 18
0
func init() {
	log.Set(
		log.NewStdLogger(),
	)
}
Exemplo n.º 19
0
func init() {
	directory.PkgLog = log.NewStdLogger()
	directory.PkgLog.SetLevel(log.StdLevelDebug)
}