开发者

Which output method shoud I use for logging system?

开发者 https://www.devze.com 2023-03-15 13:46 出处:网络
I am working on a server programmi开发者_StackOverflow中文版ng on linux machine using epoll. I am printing log statements using printf(), but I was wondering which output method uses the least system

I am working on a server programmi开发者_StackOverflow中文版ng on linux machine using epoll. I am printing log statements using printf(), but I was wondering which output method uses the least system resources.

As you guys already know, server programming deals with a lot of log statements so I was wondering the best output method I should use. Possible candidates are printf cout etc..

Thanks in advance.


Both printf and cout are console output methods, neither is going to trouble your CPU greatly, you are probably worrying about nothing.

However C++ provides three standard output streams - cout, cerr, and clog. I suggest that you should use the latter since that is what it is for, and the output of your log is then system defined.

If you do not need output formatting, then it will be marginally (and probably not measurably) faster to use fwrite(), puts(), or clog.write()


If you are using C++, You can use log4cxx for logging. It is a pretty decent framework, though you may think of simply getting around using crude or self implemented logging techniques in the long run a decent logging framework can save a lot of trouble when your project goes bigger and bigger.


Not sure of the performance, but this may work (untested) I've heard using /n is faster than std::endl too

ofstream logfile;
logfile.open("log.txt");
log <<"log text";


If your application create a lot of logs and logging statements are affecting performance, a quick solution would be to create a small process that has a queue which can be filled over a socket connection. The logging process can write to disk at regular intervals or when its input queue is full.

If even the extra disk access is affecting performance, then the logging process can be run on a smaller server connected to the same switch as the app server.

Or if you don't want the message passing latency and don't mind writing slightly more complex code, you can use a logging thread instead.


Why are you not using syslog? I think that's the obvious answer to your question, unless you have some compelling reason against it.

0

精彩评论

暂无评论...
验证码 换一张
取 消