真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

go+語言日志庫 go語言日志庫

Go語言之log(如何將日志寫到指定文件里面)

對于Go語言的日志來說,如何將log寫到指定的文件里面,下面是一個例子。

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供尤溪網站建設、尤溪做網站、尤溪網站設計、尤溪網站制作等企業(yè)網站建設、網頁設計與制作、尤溪企業(yè)網站模板建站服務,十余年尤溪做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。

output:

output:

go 的標準日志庫 Log

我們常使用 Go log 以下三組函數:

std 使用 log.New 構造出來,三個參數:

而日志輸出位置,自定義前綴字符串,flag 這三者都能夠自定義:

基本的 log 至少還需要輸出不同的日志級別,所以可以基于標準庫的 Log庫 簡單封裝一個支持不同日志級別輸出的日志。

測試:

輸出日志格式:

GO Log統(tǒng)一接口

go日志庫有很多:zap、 Logrus等,它們沒有統(tǒng)一的接口。作為中間件提供方,中間件的日志怎么輸出到業(yè)務的日志文件中?

解決方式:統(tǒng)一日志接口,類似于java中的slf4j。

1)統(tǒng)一接口層面

2)日志實現橋接到接口

sofa-mons有個pkg/log/proxylog.go文件,定義了log的代理。

1)接口

在接口層面,統(tǒng)一增加context參數,方便日志打印時獲取通用參數(比如:trace信息)。

如何使用Google日志庫

如何使用Google日志庫 (glog)

介紹

Google glog是一個應用層的庫. 它提供基于C++風格的流和多種宏接口.例如:

#include glog/logging.h

int main(int argc, char* argv[]) {

// Initialize Google's logging library.

google::InitGoogleLogging(argv[0]);

// ...

LOG(INFO) "Found " num_cookies " cookies";

}

Google glog定義了一系列的宏處理普通的日志工作. 你可以分級記錄, control loggingbehavior from the command line, 按條件記錄, abort theprogram when expected conditions are not met, introduce your ownverbose logging levels, 及其它. 文本只是簡單的介紹了常用的glog功能而不是所有的細節(jié).沒介紹到的,自己讀代碼去吧.

嚴重等級

嚴重等級包括(按嚴重程度排序): INFO, WARNING,ERROR,和FATAL.記錄一個FATAL級的錯誤后會自動終止程序執(zhí)行(其實就是_asm int 3).一個嚴重等級高的記錄不但記錄在它自己的文件,也記錄在比它低等的文件里.例如, 一個FATAL級的記錄將會記在FATAL, ERROR,WARNING, 和INFO等級的日志里.

在debug狀態(tài)下FATAL級記錄也可以用DFATAL(當沒定義NDEBUG宏的時候),發(fā)給客戶的程序最好不要用FATAL小心客戶對你使用_asm int 3,這時ERROR是個不錯的選擇.

glog默認把日志文件命名為"/tmp/...log...."(例如, "/tmp/hello_world.example.com.hamaji.log.INFO.20080709-222411.10474").glog默認把ERROR 和 FATAL 級的記錄發(fā)送一份到stderr.

設置標志

標志可以改變glog的輸出行為.如果你安裝了Googlegflags library, configure 腳本 (具體請參考它的INSTALL文件)會自動檢測并使用標志,這時你可以通過命令行使用標志.例如, 若你想發(fā)送 --logtostderr 標志,像下面這樣:

./your_application --logtostderr=1

如果你沒裝Google gflags library,你就只能用帶GLOG_的環(huán)境變量實現,例如.

GLOG_logtostderr=1 ./your_application (貌似在Windows里不能這樣用吧?)

常用的標志有:

logtostderr (bool, default=false)

寫日志到stderr而不是日志文件.

Note: you can set binary flags to true by specifying1, true, or yes (caseinsensitive).Also, you can set binary flags to false by specifying0, false, or no (again, caseinsensitive).

stderrthreshold (int, default=2, whichis ERROR)

將某級及以上級別的記錄同時發(fā)送到stderr和日志文件. 嚴重等級INFO, WARNING, ERROR, FATAL 分別對應 0, 1, 2, 3.

minloglevel (int, default=0, whichis INFO)

只記錄某級及以上級別的記錄.

log_dir (string, default="")

指定日志保存的目錄.

v (int, default=0)

Show all VLOG(m) messages for m less orequal the value of this flag. Overridable by --vmodule.See the section about verbose logging for moredetail.

vmodule (string, default="")

Per-module verbose level. The argument has to contain acomma-separated list of =.is a glob pattern (e.g., gfs* for all modules whose namestarts with "gfs"), matched against the filename base(that is, name ignoring .cc/.h./-inl.h). overrides any value given by --v.See also the section about verbose logging.

還有一些其它的標志,自己去logging.cc里面搜索"DEFINE_",爺就不多說了.

你也可以在程序里修改FLAGS_* 開頭的全局變量. 通常設置FLAGS_*開頭的變量會立即生效,和日志文件名或路徑相關的變量例外.例如FLAGS_log_dir就不能放在google::InitGoogleLogging后面.例子代碼:

LOG(INFO) "file";

// Most flags work immediately after updating values.

FLAGS_logtostderr = 1;

LOG(INFO) "stderr";

FLAGS_logtostderr = 0;

// This won't change the log destination. If you want to set this

// value, you should do this before google::InitGoogleLogging .

FLAGS_log_dir = "/some/log/directory";

LOG(INFO) "the same file";

條件/ Occasional Logging

下面的宏用于根據條件寫日志:

LOG_IF(INFO, num_cookies 10) "Got lots of cookies";

只有當num_cookies 10的時候才會記錄"Got lots of cookies"

如果有的代碼執(zhí)行非常頻繁,你肯定不會希望每次都寫入日志.這時,你可以使用下面的方法:

LOG_EVERY_N(INFO, 10) Got the googleCOUNTER th cookiepre

上面的代碼只有當第1次,第11次,第21次....執(zhí)行時才寫入日志.Note that the specialgoogle::COUNTER value is used to identify which repetition ishappening.

條件和計次可以混合使用,例如:

LOG_IF_EVERY_N(INFO, (size 1024), 10) "Got the " google::COUNTER

"th big cookie";

除了間隔N次輸出,還可以只輸出前M次,例如:

LOG_FIRST_N(INFO, 20) Got the googleCOUNTER th cookiepre

只輸出前20次.

Debug mode支持

"debug mode"的宏只在調試模式有效,其他模式不生效.上栗子:

DLOG(INFO) "Found cookies";

DLOG_IF(INFO, num_cookies 10) "Got lots of cookies";

DLOG_EVERY_N(INFO, 10) "Got the " google::COUNTER "th cookie";

CHECK宏

經常檢查是否會出錯而不是等著程序執(zhí)行出錯是個不錯的習慣.CHECK宏就是干這個滴,它的功能和assert一樣,條件不符合時終止程序.

與assert不同的是它*不*受NDEBUG的限制,無論是不是debug編譯它都執(zhí)行.所以下面的fp-Write(x)會被執(zhí)行:

CHECK(fp-Write(x) == 4) "Write failed!";

有各種各樣的宏用于CHECK相等/不相等,包括: CHECK_EQ,CHECK_NE, CHECK_LE, CHECK_LT,CHECK_GE,和CHECK_GT.它們會記錄一個FATAL級的記錄到日志,牛X的是他們還會把對比的兩個值也一起記錄.(那兩個值必須定義了 operator ostreamcode ).例如:

CHECK_NE(1, 2) ": The world must be ending!";

We are very careful to ensure that each argument is evaluated exactlyonce, and that anything which is legal to pass as a function argument islegal here. In particular, the arguments may be temporary expressionswhich will end up being destroyed at the end of the apparent statement,for example:

CHECK_EQ(string("abc")[1], 'b');

The compiler reports an error if one of the arguments is apointer and the other is NULL. To work around this, simply static_castNULL to the type of the desired pointer.

CHECK_EQ(some_ptr, static_cast(NULL));

還有個好辦法:用CHECK_NOTNULL宏:

CHECK_NOTNULL(some_ptr); some_ptr-DoSomething();

這宏也常用于構造函數中.

struct S {

S(Something* ptr) : ptr_(CHECK_NOTNULL(ptr)) {}

Something* ptr_;

};

這宏的缺點是不能跟C++流一起用.這時就得用CHECK_EQ了.

如果你要對比一個C字符串(char *)可以用:CHECK_STREQ, CHECK_STRNE,CHECK_STRCASEEQ,和CHECK_STRCASENE.帶CASE的區(qū)分大小寫. 這個宏參數可以用NULL指針.NULL與non-NULL比不等.兩個NULL是相等的.

Note that both arguments may be temporary strings which aredestructed at the end of the current "full expression"(e.g., CHECK_STREQ(Foo().c_str(), Bar().c_str()) whereFoo and Bar return C++'sstd::string).

CHECK_DOUBLE_EQ 用于對比浮點數,會有小小的誤差.CHECK_NEAR 可以接受一個浮點數作為誤差.

詳細日志

用VLOG宏,你還可以定義自己的嚴重等級. The --v command line option controlswhich verbose messages are logged:

VLOG(1) "I'm printed when you run the program with --v=1 or higher";

VLOG(2) "I'm printed when you run the program with --v=2 or higher";

With VLOG, the lower the verbose level, the morelikely messages are to be logged. For example, if--v==1, VLOG(1) will log, butVLOG(2) will not log. This is opposite of the severitylevel, where INFO is 0, and ERROR is 2.--minloglevel of 1 will log WARNING andabove. Though you can specify any integers for both VLOGmacro and --v flag, the common values for them are smallpositive integers. For example, if you write VLOG(0),you should specify --v=-1 or lower to silence it. Thisis less useful since we may not want verbose logs by default in mostcases. The VLOG macros always log at theINFO log level (when they log at all).

Verbose logging can be controlled from the command line on aper-module basis:

--vmodule=mapreduce=2,file=1,gfs*=3 --v=0

will:

a. Print VLOG(2) and lower messages from mapreduce.{h,cc}

b. Print VLOG(1) and lower messages from file.{h,cc}

c. Print VLOG(3) and lower messages from files prefixed with "gfs"

d. Print VLOG(0) and lower messages from elsewhere

The wildcarding functionality shown by (c) supports both '*'(matches 0 or more characters) and '?' (matches any single character)wildcards. Please also check the section about command line flags.

There's also VLOG_IS_ON(n) "verbose level" conditionmacro. This macro returns true when the --v is equal orgreater than n. To be used as

if (VLOG_IS_ON(2)) {

// do some logging preparation and logging

// that can't be accomplished with just VLOG(2) ...;

}

Verbose level condition macros VLOG_IF,VLOG_EVERY_N and VLOG_IF_EVERY_N behaveanalogous to LOG_IF, LOG_EVERY_N,LOF_IF_EVERY, but accept a numeric verbosity level asopposed to a severity level.

VLOG_IF(1, (size 1024))

"I'm printed when size is more than 1024 and when you run the "

"program with --v=1 or more";

VLOG_EVERY_N(1, 10)

"I'm printed every 10th occurrence, and when you run the program "

"with --v=1 or more. Present occurence is " google::COUNTER;

VLOG_IF_EVERY_N(1, (size 1024), 10)

"I'm printed on every 10th occurence of case when size is more "

" than 1024, when you run the program with --v=1 or more. ";

"Present occurence is " google::COUNTER;

Failure Signal Handler

The library provides a convenient signal handler that will dump usefulinformation when the program crashes on certain signals such as SIGSEGV.The signal handler can be installed bygoogle::InstallFailureSignalHandler(). The following is an example of outputfrom the signal handler.

*** Aborted at 1225095260 (unix time) try "date -d @1225095260" if you are using GNU date ***

*** SIGSEGV (@0x0) received by PID 17711 (TID 0x7f893090a6f0) from PID 0; stack trace: ***

PC: @ 0x412eb1 TestWaitingLogSink::send()

@ 0x7f892fb417d0 (unknown)

@ 0x412eb1 TestWaitingLogSink::send()

@ 0x7f89304f7f06 google::LogMessage::SendToLog()

@ 0x7f89304f35af google::LogMessage::Flush()

@ 0x7f89304f3739 google::LogMessage::~LogMessage()

@ 0x408cf4 TestLogSinkWaitTillSent()

@ 0x4115de main

@ 0x7f892f7ef1c4 (unknown)

@ 0x4046f9 (unknown)

By default, the signal handler writes the failure dump to the standarderror. You can customize the destination by InstallFailureWriter().

Miscellaneous Notes

Performance of Messages

The conditional logging macros provided by glog (e.g.,CHECK, LOG_IF, VLOG, ...) arecarefully implemented and don't execute the right hand sideexpressions when the conditions are false. So, the following checkmay not sacrifice the performance of your application.

CHECK(obj.ok) obj.CreatePrettyFormattedStringButVerySlow();

User-defined Failure Function

FATAL severity level messages or unsatisfiedCHECK condition terminate your program. You can changethe behavior of the termination byInstallFailureFunction.

void YourFailureFunction() {

// Reports something...

exit(1);

}

int main(int argc, char* argv[]) {

google::InstallFailureFunction(YourFailureFunction);

}

By default, glog tries to dump stacktrace and makes the programexit with status 1. The stacktrace is produced only when you run theprogram on an architecture for which glog supports stack tracing (asof September 2008, glog supports stack tracing for x86 and x86_64).

Raw Logging

The header file can beused for thread-safe logging, which does not allocate any memory oracquire any locks. Therefore, the macros defined in thisheader file can be used by low-level memory allocation andsynchronization code.Please check src/glog/raw_logging.h.in for detail.

Google Style perror()

PLOG() and PLOG_IF() andPCHECK() behave exactly like their LOG* andCHECK equivalents with the addition that they append adescription of the current state of errno to their output lines.E.g.

PCHECK(write(1, NULL, 2) = 0) Write NULL failedpre

This check fails with the following error message.

F0825 185142 test.cc:22] Check failed: write(1, NULL, 2) = 0 Write NULL failed: Bad address [14]

Syslog

SYSLOG, SYSLOG_IF, andSYSLOG_EVERY_N macros are available.These log to syslog in addition to the normal logs. Be aware thatlogging to syslog can drastically impact performance, especially ifsyslog is configured for remote logging! Make sure you understand theimplications of outputting to syslog before you use these macros. Ingeneral, it's wise to use these macros sparingly.

Strip Logging Messages

Strings used in log messages can increase the size of your binaryand present a privacy concern. You can therefore instruct glog toremove all strings which fall below a certain severity level by usingthe GOOGLE_STRIP_LOG macro:

If your application has code like this:

#define GOOGLE_STRIP_LOG 1 // this must go before the #include!

#include glog/logging.h

The compiler will remove the log messages whose severities are lessthan the specified integer value. SinceVLOG logs at the severity level INFO(numeric value 0),setting GOOGLE_STRIP_LOG to 1 or greater removesall log messages associated with VLOGs as well asINFO log statements.

對于Windows用戶

glog的ERROR級錯誤與windows.h沖突. You can make glog not defineINFO, WARNING, ERROR,and FATAL by definingGLOG_NO_ABBREVIATED_SEVERITIES beforeincluding glog/logging.h . Even with this macro, you canstill use the iostream like logging facilities:

#define GLOG_NO_ABBREVIATED_SEVERITIES

#include windows.h

#include glog/logging.h

// ...

LOG(ERROR) "This should work";

LOG_IF(ERROR, x y) "This should be also OK";

However, you cannotuse INFO, WARNING, ERROR,and FATAL anymore for functions definedin glog/logging.h .

#define GLOG_NO_ABBREVIATED_SEVERITIES

#include windows.h

#include glog/logging.h

// ...

// This won't work.

// google::FlushLogFiles(google::ERROR);

// Use this instead.

google::FlushLogFiles(google::GLOG_ERROR);

If you don't need ERROR definedby windows.h, there are a couple of more workaroundswhich sometimes don't work:

#define WIN32_LEAN_AND_MEAN or NOGDIbefore you #include windows.h .

#undef ERRORafter you #include windows.h .

See this issue for more detail.


網頁題目:go+語言日志庫 go語言日志庫
文章地址:http://weahome.cn/article/dohjieh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部