• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang log.EnableLogFileOutput函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/cockroachdb/cockroach/util/log.EnableLogFileOutput函数的典型用法代码示例。如果您正苦于以下问题:Golang EnableLogFileOutput函数的具体用法?Golang EnableLogFileOutput怎么用?Golang EnableLogFileOutput使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了EnableLogFileOutput函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestStatusLocalLogs

// TestStatusLocalLogs checks to ensure that local/logfiles,
// local/logfiles/{filename}, local/log and local/log/{level} function
// correctly.
func TestStatusLocalLogs(t *testing.T) {
	defer leaktest.AfterTest(t)
	dir, err := ioutil.TempDir("", "local_log_test")
	if err != nil {
		t.Fatal(err)
	}
	log.EnableLogFileOutput(dir)
	defer func() {
		log.DisableLogFileOutput()
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}()

	ts := startServer(t)
	defer ts.Stop()

	// Log an error which we expect to show up on every log file.
	timestamp := time.Now().UnixNano()
	log.Errorf("TestStatusLocalLogFile test message-Error")
	timestampE := time.Now().UnixNano()
	log.Warningf("TestStatusLocalLogFile test message-Warning")
	timestampEW := time.Now().UnixNano()
	log.Infof("TestStatusLocalLogFile test message-Info")
	timestampEWI := time.Now().UnixNano()

	type logsWrapper struct {
		Data []log.FileInfo `json:"d"`
	}
	var logs logsWrapper
	if err := json.Unmarshal(getRequest(t, ts, "/_status/logfiles/local"), &logs); err != nil {
		t.Fatal(err)
	}
	if a, e := len(logs.Data), 3; a != e {
		t.Fatalf("expected %d log files; got %d", e, a)
	}
	for i, name := range []string{"log.ERROR", "log.INFO", "log.WARNING"} {
		if !strings.Contains(logs.Data[i].Name, name) {
			t.Errorf("expected log file name %s to contain %q", logs.Data[i].Name, name)
		}
	}

	// Fetch the full list of log entries.
	type logWrapper struct {
		Data []log.LogEntry `json:"d"`
	}

	// Check each individual log can be fetched and is non-empty.
	var foundInfo, foundWarning, foundError bool
	for _, file := range logs.Data {
		var log logWrapper
		if err := json.Unmarshal(getRequest(t, ts, fmt.Sprintf("/_status/logfiles/local/%s", file.Name)), &log); err != nil {
			t.Fatal(err)
		}
		for _, entry := range log.Data {
			switch entry.Format {
			case "TestStatusLocalLogFile test message-Error":
				foundError = true
			case "TestStatusLocalLogFile test message-Warning":
				foundWarning = true
			case "TestStatusLocalLogFile test message-Info":
				foundInfo = true
			}
		}
	}

	if !(foundInfo && foundWarning && foundError) {
		t.Errorf("expected to find test messages in %v", logs.Data)
	}

	testCases := []struct {
		Level           log.Severity
		MaxEntities     int
		StartTimestamp  int64
		EndTimestamp    int64
		Pattern         string
		ExpectedError   bool
		ExpectedWarning bool
		ExpectedInfo    bool
	}{
		// Test filtering by log severity.
		{log.InfoLog, 0, 0, 0, "", true, true, true},
		{log.WarningLog, 0, 0, 0, "", true, true, false},
		{log.ErrorLog, 0, 0, 0, "", true, false, false},
		// Test entry limit. Ignore Info/Warning/Error filters.
		{log.InfoLog, 1, timestamp, timestampEWI, "", false, false, false},
		{log.InfoLog, 2, timestamp, timestampEWI, "", false, false, false},
		{log.InfoLog, 3, timestamp, timestampEWI, "", false, false, false},
		// Test filtering in different timestamp windows.
		{log.InfoLog, 0, timestamp, timestamp, "", false, false, false},
		{log.InfoLog, 0, timestamp, timestampE, "", true, false, false},
		{log.InfoLog, 0, timestampE, timestampEW, "", false, true, false},
		{log.InfoLog, 0, timestampEW, timestampEWI, "", false, false, true},
		{log.InfoLog, 0, timestamp, timestampEW, "", true, true, false},
		{log.InfoLog, 0, timestampE, timestampEWI, "", false, true, true},
		{log.InfoLog, 0, timestamp, timestampEWI, "", true, true, true},
		// Test filtering by regexp pattern.
//.........这里部分代码省略.........
开发者ID:kaustubhkurve,项目名称:cockroach,代码行数:101,代码来源:status_test.go


示例2: TestStatusLocalLogs

// TestStatusLocalLogs checks to ensure that local/logfiles,
// local/logfiles/{filename}, local/log and local/log/{level} function
// correctly.
func TestStatusLocalLogs(t *testing.T) {
	defer leaktest.AfterTest(t)
	dir, err := ioutil.TempDir("", "local_log_test")
	if err != nil {
		t.Fatal(err)
	}
	log.EnableLogFileOutput(dir)
	defer func() {
		log.DisableLogFileOutput()
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}()

	ts := startServer(t, statusLocalLogFileKeyPrefix)
	defer ts.Stop()

	// Log an error which we expect to show up on every log file.
	timestamp := time.Now().UnixNano()
	log.Errorf("TestStatusLocalLogFile test message-Error")
	timestampE := time.Now().UnixNano()
	log.Warningf("TestStatusLocalLogFile test message-Warning")
	timestampEW := time.Now().UnixNano()
	log.Infof("TestStatusLocalLogFile test message-Info")
	timestampEWI := time.Now().UnixNano()

	body := getRequest(t, ts, statusLocalLogFileKeyPrefix)

	type logsWrapper struct {
		Data []log.FileInfo `json:"d"`
	}
	logs := logsWrapper{}
	if err := json.Unmarshal(body, &logs); err != nil {
		t.Fatal(err)
	}
	if a, e := len(logs.Data), 3; a != e {
		t.Fatalf("expected %d log files; got %d", e, a)
	}
	for i, name := range []string{"log.ERROR", "log.INFO", "log.WARNING"} {
		if !strings.Contains(logs.Data[i].Name, name) {
			t.Errorf("expected log file name %s to contain %q", logs.Data[i].Name, name)
		}
	}

	// Fetch a each listed log directly.
	type logWrapper struct {
		Data []log.LogEntry `json:"d"`
	}
	// Check each individual log can be fetched and is non-empty.
	for _, log := range logs.Data {
		body = getRequest(t, ts, fmt.Sprintf("%s%s", statusLocalLogFileKeyPrefix, log.Name))
		logW := logWrapper{}
		if err := json.Unmarshal(body, &logW); err != nil {
			t.Fatal(err)
		}
		var found bool
		for _, data := range logW.Data {
			if data.Format == "TestStatusLocalLogFile test message-Error" {
				found = true
				break
			}
		}
		if !found {
			t.Errorf("expected to find test message in %v", logW.Data)
		}
	}

	// Fetch the full list of log entries.
	type entryWrapper struct {
		Data []log.LogEntry `json:"d"`
	}

	testCases := []struct {
		Level           log.Severity
		MaxEntities     int
		StartTimestamp  int64
		EndTimestamp    int64
		Pattern         string
		ExpectedError   bool
		ExpectedWarning bool
		ExpectedInfo    bool
	}{
		// Test filtering by log severity.
		{log.InfoLog, 0, 0, 0, "", true, true, true},
		{log.WarningLog, 0, 0, 0, "", true, true, false},
		{log.ErrorLog, 0, 0, 0, "", true, false, false},
		// Test filtering in different timestamp windows.
		{log.InfoLog, 1, timestamp, timestampEWI, "", true, false, false},
		{log.InfoLog, 2, timestamp, timestampEWI, "", true, true, false},
		{log.InfoLog, 3, timestamp, timestampEWI, "", true, true, true},
		{log.InfoLog, 0, timestamp, timestamp, "", false, false, false},
		{log.InfoLog, 0, timestamp, timestampE, "", true, false, false},
		{log.InfoLog, 0, timestampE, timestampEW, "", false, true, false},
		{log.InfoLog, 0, timestampEW, timestampEWI, "", false, false, true},
		{log.InfoLog, 0, timestamp, timestampEW, "", true, true, false},
		{log.InfoLog, 0, timestampE, timestampEWI, "", false, true, true},
		{log.InfoLog, 0, timestamp, timestampEWI, "", true, true, true},
//.........这里部分代码省略.........
开发者ID:zhaoyta,项目名称:cockroach,代码行数:101,代码来源:status_test.go


示例3: TestStatusLocalLogs

// TestStatusLocalLogs checks to ensure that local/logfiles,
// local/logfiles/{filename}, local/log and local/log/{level} function
// correctly.
func TestStatusLocalLogs(t *testing.T) {
	defer leaktest.AfterTest(t)
	dir, err := ioutil.TempDir("", "local_log_test")
	if err != nil {
		t.Fatal(err)
	}
	log.EnableLogFileOutput(dir)
	defer func() {
		log.DisableLogFileOutput()
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}()
	ts, body := startServerAndGetStatus(t, statusLocalLogFileKeyPrefix)
	defer ts.Stop()

	type logsWrapper struct {
		Data []log.FileInfo `json:"d"`
	}
	logs := logsWrapper{}
	if err := json.Unmarshal(body, &logs); err != nil {
		t.Fatal(err)
	}
	if l := len(logs.Data); l != 3 {
		t.Fatalf("expected 3 log files; got %d", l)
	}
	for i, pat := range []string{`.*log.ERROR.*`, `.*log.INFO.*`, `.*log.WARNING.*`} {
		if ok, err := regexp.MatchString(pat, logs.Data[i].Name); !ok || err != nil {
			t.Errorf("expected log file %s to match %q: %s", logs.Data[i].Name, pat, err)
		}
	}

	// Log an error which we expect to show up on every log file.
	timestamp := time.Now().UnixNano()
	log.Errorf("TestStatusLocalLogFile test message-Error")
	timestampE := time.Now().UnixNano()
	log.Warningf("TestStatusLocalLogFile test message-Warning")
	timestampEW := time.Now().UnixNano()
	log.Infof("TestStatusLocalLogFile test message-Info")
	timestampEWI := time.Now().UnixNano()

	// Fetch a each listed log directly.
	type logWrapper struct {
		Data []log.LogEntry `json:"d"`
	}
	// Check each individual log can be fetched and is non-empty.
	for _, log := range logs.Data {
		body = getRequest(t, ts, fmt.Sprintf("%s%s", statusLocalLogFileKeyPrefix, log.Name))
		logW := logWrapper{}
		if err := json.Unmarshal(body, &logW); err != nil {
			t.Fatal(err)
		}
		var found bool
		for i := len(logW.Data) - 1; i >= 0; i-- {
			if logW.Data[i].Format == "TestStatusLocalLogFile test message-Error" {
				found = true
				break
			}
		}
		if !found {
			t.Errorf("expected to find test message in %v", logW.Data)
		}
	}

	// Fetch the full list of log entries.
	type entryWrapper struct {
		Data []log.LogEntry `json:"d"`
	}

	testCases := []struct {
		Level           log.Severity
		MaxEntities     int
		StartTimestamp  int64
		EndTimestamp    int64
		ExpectedError   bool
		ExpectedWarning bool
		ExpectedInfo    bool
	}{
		{log.InfoLog, 0, 0, 0, true, true, true},
		{log.WarningLog, 0, 0, 0, true, true, false},
		{log.ErrorLog, 0, 0, 0, true, false, false},
		{log.InfoLog, 1, timestamp, timestampEWI, true, false, false},
		{log.InfoLog, 2, timestamp, timestampEWI, true, true, false},
		{log.InfoLog, 3, timestamp, timestampEWI, true, true, true},
		{log.InfoLog, 0, timestamp, timestamp, false, false, false},
		{log.InfoLog, 0, timestamp, timestampE, true, false, false},
		{log.InfoLog, 0, timestampE, timestampEW, false, true, false},
		{log.InfoLog, 0, timestampEW, timestampEWI, false, false, true},
		{log.InfoLog, 0, timestamp, timestampEW, true, true, false},
		{log.InfoLog, 0, timestampE, timestampEWI, false, true, true},
		{log.InfoLog, 0, timestamp, timestampEWI, true, true, true},
	}

	for i, testCase := range testCases {
		var url bytes.Buffer
		fmt.Fprintf(&url, "%s%s?", statusLocalLogKeyPrefix, testCase.Level.Name())
		if testCase.MaxEntities > 0 {
//.........这里部分代码省略.........
开发者ID:Gardenya,项目名称:cockroach,代码行数:101,代码来源:status_test.go



注:本文中的github.com/cockroachdb/cockroach/util/log.EnableLogFileOutput函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang log.Error函数代码示例发布时间:2022-05-23
下一篇:
Golang log.DisableLogFileOutput函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap