Tag Info

New answers tagged

0

Instead of thinking of logging (i.e. your application telling some logging system, perhaps using syslog(3) on Linux, what is going on) you might think of it differently: your application could be a server for requests querying its state. Then you might even make your app some JSONRPC server (and you'll have also to code the tiny command-line client to ...


6

You are correct that all of these solutions dirty your code with logging / reporting code that breaks separation of concerns. This sort of problem is exactly why Aspect Oriented Programming exists: Wikipedia (emphasis mine): Aspect-oriented programming entails breaking down program logic into distinct parts (so-called concerns, cohesive areas of ...


4

I usually do this by letting every long-running class implement an interface, e.g. LongRunning, with a single method reportProgress(). Your logging service would simply be notified of the existence of such long runners, periodically query their status, and then report on the general state of the world in whatever way it sees fit. Your business code still ...


1

Of course it's a violation of SRP as you have a cross cutting concern. You can however create a class that is responsible for composing the logging with execution of any action. example: class Logger { ActuallLogger logger; public Action ComposeLog(string msg, Action action) { return () => { logger.debug(msg); action(); ...


5

This sounds fine. You're describing a fairly standard logging decorator. You have: component L (logging component of the system) This has one responsibility: logging information that is passed to it. component A implements I This has one responsibility: providing an implementation of interface I (assuming I is properly SRP-compliant, that is). ...


-3

Yes it is a violation of SRP as logging is a cross cutting concern. The correct way is to delegate logging to a logger class (Interception) which sole purpose is to log - abiding by the SRP. See this link for a good example: https://msdn.microsoft.com/en-us/library/dn178467%28v=pandp.30%29.aspx Here is a short example: public interface ITenantStore { ...


14

No, it is not a violation of SRP. The messages you send to the log should change for the same reasons as the surrounding code. What IS a violation of SRP is using a specific library for logging directly in the code. If you decide to change the way of logging, SRP states that it should not impact your business code. Some kind of abstract Logger should be ...


60

I would say you're taking SRP far too seriously. If your code is tidy enough that logging is the only "violation" of SRP then you are doing better than 99% of all other programmers, and you should pat yourself on the back. The point of SRP is to avoid horrific spaghetti code where code that does different things is all mixed up together. Mixing logging with ...


7

As logging is often considered a cross-cutting concern I'd suggest using AOP for separating logging from implementation. Depending on the language you'd use an interceptor or some AOP framework (e.g. AspectJ in Java) to perform this. The question is if this is actually worth the hassle. Note that this separation will increase the complexity of your ...


1

It depends on what that string you're passing in actually does. For this answer, I'm going to assume that string is a "scope prefix", i.e. a string that simply gets printed out at the beginning of every log message output via this logger to make it clear where it comes from, because that's how every logger I've ever worked with did things (if it does do ...



Top 50 recent answers are included