If I want to build a logger class in C ++ or java, which one should it be single or static

General question: I like to create a log class that writes to a single log file from different classes in my application, what should a single or static class be a registrar class

+4
source share
6 answers

In C ++, you need a singleton, not a static one. C ++ does not allow you to control the order in which static objects are built and destroyed, and if you tried to write before the static behavior was built, it might be undefined.

I am not sure about Java.

+3
source

Why do you think it should be? What about a regular non-static class that can be created on demand? And then create the only static instance available as the default registrar. Thus, you get the best of both worlds: convenient global access to the logger and the ability to test or temporarily use another registrar.

Another suggestion is to simply create one instance and pass it as a parameter to each component of your class, as @disown suggests.

But if you make the class itself static or solitary, you simply shoot in the foot.

Edit
For example, in response to @Stephen's comment:

// define a logger class, a perfectly ordinary class, not a singleton, and without all static members class Logger { // .... }; // create a single global *instance* of this class Logger defaultLog; void foo() { // and now use the globally visible instance defaultLog.Log("hello"); // or create a new one if that what we need: Logger newlog; newlog.Log("hello"); } 

There is no magic. This is exactly what the standard library does. std::cout not single. This is just a global instance of the std::ostream class, a class that can also be created in the usual way, if and when you need it.

+8
source

The answer, of course, is "it depends."

First of all, if I were you, I would not have invented the wheel and just used Log4j . If this does not meet your exact requirement, you are probably better off not extending a single log4net component that does not meet your needs (e.g. a custom log source) than starting from scratch. Secondly, a static class can be good enough for a simple logging class. For something more complicated, a single-class method might turn out to be.

0
source

In C ++, you would use this idiom for lazy initialization:

 Foo& getInstance() { static Foo instance; return instance; } 
0
source

Do not use singleton or static, use Injection Dependency, i.e. create an instance of one instance in your main () and pass a link to this one instance to all dependent classes. Static and / or singleton are almost never needed and often lead to less elegant code.

-1
source

I would probably use a singleton, but in any case I would hide it behind a function or macro. Do not make people type

 MySuperSpectacularLogger::GetInstance()->Log("adssdf"); 

Use instead:

 void Log(cpnst string& msg) { MySuperSpectacularLogger::GetInstance()->Log(msg); } 

or even better:

 // Logs 'msg' with the filename and line number that generates it. #define LOG(msg) \ MySuperSpectacularLogger::GetInstance()->Log(__FILE__, __LINE__, msg); 
-1
source

Source: https://habr.com/ru/post/1305962/


All Articles