简体   繁体   中英

Simple Redirection of outputs vs. Implementing a logger

I wonder what is the virtue in implementing a logger when you can actually redirect your program output into a file: runMe > myFile.log

I can think of reasons like " thread-safety ", " severity levels ", etc, but still, I'm not that convinced when a logger is explicitely needed.

(If "thread safety" is a reason and there are no severity levels, could you say that a single-threaded program doesn't need a logger and can handle just fine with just redirection-into-a-file?)

If "thread safety" is a reason and there are no severity levels, could you say that a single-threaded program doesn't need a logger and can handle just fine with just redirection-into-a-file?

IMO, yes. A single-thread program where all output is weighted equally (no severities) may serve just as well to redirect output as to implement an actual logger (and do so with less overhead).

That said, this assumes that your program doesn't provide any meaningful output; logs only. Otherwise, you've only got yourself stderr and stdout for your logging and your regular output. Once the program grows, you'll likely find yourself implementing severities.

Loggers also simplify logging to multiple locations, network locations (in some cases), etc. These can be achieved from the command line with enough shenanigans, but are something to consider.

TL;DR: yes, you can do it with redirection if you want, but if your program grows too much, you'll likely find yourself wanting severities, so make all your calls to something consistent that you could replace with a logger if the need arises.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM