Slashdot Mirror


Software Logging Schemes?

MySkippy writes "I've been a software engineer for just over 10 years, and I've seen a lot of different styles of logging in the applications I've worked on. Some were extremely verbose — about 1 logging line for every 2 lines of code. Others were very lacking, with maybe 1 line in 200 devoted to logging. I personally find that writing debug and informational messages about every 2 to 5 lines works well for debugging an issue, but can become cumbersome when reading through a log for analysis. I like to write warning messages when thresholds or limits are being approached — these tend to be infrequent. I log errors whenever I catch one (but I've never put a 'fatal' message in my code, because if it's truly a fatal error I probably didn't catch it). Recently I came across log4j and log4net and have begun using them both. That brings me to my question: how do the coders on Slashdot handle logging in their code?"

225 comments

  1. As little as practically possible by the+eric+conspiracy · · Score: 0

    Log the starting conditions so you can reconstruct data. Otherwise don't do much logging because it will hurt application performance, sometimes drastically.

    1. Re:As little as practically possible by 0123456 · · Score: 5, Insightful

      "Otherwise don't do much logging because it will hurt application performance, sometimes drastically."

      You're assuming that performance -- or, more precisely, CPU usage -- is important; in many cases, reliability (and being able to track down bugs after a crash) are far more important than CPU usage. With quad-core CPUs so cheap these days, we can easily afford to spend another thousand dollars to throw more processing power into a system which has cost a couple of hundred thousand dollars of programmer time to develop and will cost thousands of dollars an hour for any downtime.

    2. Re:As little as practically possible by dslauson · · Score: 4, Insightful

      "You're assuming that performance -- or, more precisely, CPU usage -- is important; in many cases, reliability (and being able to track down bugs after a crash) are far more important than CPU usage."

      I work on a real-time embedded medical device, where both performance and reliability are vital. We've got constrained resources, and the system must be extremely responsive.

      Our logging scheme is pretty cool. It's written so that two computers can log to a single hard drive, and each logging statement must define a log level. So, for example, if I'm writing GUI code, I can log to log_level_gui_info, log_level_gui_debug, log_level_gui_error, or any of a number of more specific log levels.

      The idea is

      1. Some of these log levels we can turn off before a production release.
      2. We have a special tool for reading these logs (they're encrypted), and in this tool you can check off which log levels you care to see, and which you don't

      So, we have two ways to filter out extraneous logging that we don't care about (one actually keeps the logging from happening, and one just filters it out during analysis), and we can log as freely as we like as long as we're smart about which levels we're using.

      As much faith as we all have in our own code, nothing's as frustrating as trying to analyze a log that came in from the field where there's just no information about what went wrong.

    3. Re:As little as practically possible by theshowmecanuck · · Score: 1, Insightful

      You're assuming that performance -- or, more precisely, CPU usage -- is important; in many cases, reliability (and being able to track down bugs after a crash) are far more important than CPU usage.

      I wonder why, given the huge increase in the performance of computers over the last ten years and more, why it sill takes some games one to five minutes to load... one reason I am more likely to play spider solitaire now when I just want to play something for 10 or 15 minutes. Anyway... it is this kind of attitude causing it (e.g. "Screw it... we have good CPUs and lots of memory... who gives a shit if I don't feel like considering performance"). If people would program things as efficiently now as they did in years past, performance tuning analysts would be out of work, the enterprise systems I see being build now wouldn't be sucked out performance dogs out of the gate, games would be more fun, etc. etc. etc. etc.

      ok... I feel better now...

      --
      -- I ignore anonymous replies to my comments and postings.
    4. Re:As little as practically possible by the+eric+conspiracy · · Score: 1

      Turning on verbose logging doesn't help you after the process has gone tits up. It's ok if you are debugging, but really if you are debugging you want to use a debugger, not a log.

      As far as hardware being cheap etc. as other posters have posited, I'd sure like to know where you work that a Sun E10000 and an EMC Symmetrix are considered cheap.

      The fact is that if you are running apps on big iron hardware is not cheap, and the economic effectiveness of an application can well depend on how many transactions it can process in a given period of time.

      Yes, it can be tough to figure out what went wrong with a sparse log. But that's the way it is, sorry.

      On the other hand I do agree with people who state disk space is usually not a factor. There are ways to manage that issue.

    5. Re:As little as practically possible by linear+a · · Score: 2, Informative

      Afraid you're describing a very natural behavior that's unlikely to disappear. Developers (not just software) tend to work until each constraint is just met and then stop to work on the next constraint. E.g., get the load time down to the maximum acceptable time, then stop working on it.

    6. Re:As little as practically possible by JonTurner · · Score: 1

      Actually, I agree with you -- I think logging is essential. That's why I said "Joking aside..." and then went on to suggest making it configurable.

      Sorry I wasn't clearer. I might have to go back to using the "/irony" tags.

    7. Re:As little as practically possible by Champion3 · · Score: 3, Interesting

      I'd be careful about using multi-core as a crutch: if the logging code isn't written properly, it can still be a bottleneck. Last year at my office we had to excise our old logging library because it turned out that each function call to the log acquired a global critical section even if the log level threshold wasn't met. Multi core wouldn't help because it was highly likely that any given call to the log would block the calling thread.

      --
      I'm going to the casino. Don't gamble.
    8. Re:As little as practically possible by KillerBob · · Score: 1

      Log the starting conditions so you can reconstruct data. Otherwise don't do much logging because it will hurt application performance, sometimes drastically.

      It can hurt performance drastically, but you still need *some* logging. I agree about logging the start conditions. The other thing that I log while I'm debugging my code is every event which results in a function call, and a begin function & end function log entry. I don't find there's a lot of point in logging more than that while debugging my code. Not generally, at least... if something isn't working right, I can check the log to see what started and didn't complete, or what function isn't completing with expected results. I can then go to that function in my code, and if I can't figure out what's wrong with it I can add extra logging to the function to help figure it out. Once or twice, I've even had logging every line of code while debugging a particularly complex function.

      Bottom line: Keep logging to a minimum. All you really need to know is what isn't working. But be prepared to go in and add more logging if it's needed.

      --
      If you believe everything you read, you'd better not read. - Japanese proverb
    9. Re:As little as practically possible by telbij · · Score: 2, Interesting

      Afraid you're describing a very natural behavior that's unlikely to disappear. Developers (not just software) tend to work until each constraint is just met and then stop to work on the next constraint.

      And let's hope it never does disappear or we can kiss new software goodbye.

    10. Re:As little as practically possible by omeomi · · Score: 3, Insightful

      I wonder why, given the huge increase in the performance of computers over the last ten years and more, why it sill takes some games one to five minutes to load

      There are more textures, and they are stored at a higher resolution than they used to be; The 3D models have more triangles; there are more sound effects, and the quality is higher; there is more music, and the quality is higher...It takes time to load all of this into memory.

    11. Re:As little as practically possible by darkpixel2k · · Score: 5, Funny

      ...nothing's as frustrating as trying to analyze a log that came in from the field where there's just no information about what went wrong.

      [ 56.529336] WARNING: A-fib detected
      [ 56.568802] INFO: charging defib
      [ 56.741096] INFO: charging ccomplete
      [ 57.218803] ALERT: shocking!
      [ 58.061815] Buffer I/O error on /dev/paddles
      [ 58.163210] zapper[22402]: segfault at 000000c4 eip b321bf5f esp b320a870 error 6
      BUG: unable to handle kernel NULL pointer dereference at virtual adress 00000000
      printing eip: c013186b *pde = 00000000
      Oops: 0000 [#1] SMP
      Modules linked in: battery ipv6 paddles ac button battery cardiac_monitor thermal processor zapper fan

      Man, I'd feel bad after that output...

      --
      There's no place like ::1 (I've completed my transition to IPv6)
    12. Re:As little as practically possible by Dun+Malg · · Score: 2, Informative

      Afraid you're describing a very natural behavior that's unlikely to disappear. Developers (not just software) tend to work until each constraint is just met and then stop to work on the next constraint.

      And let's hope it never does disappear or we can kiss new software goodbye.

      Some of that behavior I've seen, we'd be better off if it disappeared. Sometimes, simply expanding till limits are met is like the old saying "some day, computers will be the size of buildings, and have millions of vacuum tubes!"

      Perhaps I'm just pessimistic because where I work they're in the process of replacing the huge, unwieldy FileMaker monstrosity that handles payroll.... with an even bigger, more unwieldy FileMaker monstrosity that does the same thing, but requires us to feed it more information (more detailed job codes, time tracked in 6 minute increments, etc). All this extra data will be used to generate reports explaining why we're always releasing late. Never mind that we've been badgering them to hire more people for months. No, it's clearly a time management issue! As the PHB in Dilbert once said, "I want hourly reports on why we're behind schedule!"

      --
      If a job's not worth doing, it's not worth doing right.
    13. Re:As little as practically possible by TapeCutter · · Score: 2, Insightful

      "Turning on verbose logging doesn't help you after the process has gone tits up."

      How can you demonstrate that you have fixed the reported bug if you can't recreate it in the first place?

      --
      And did you exchange a walk on part in the war for a lead role in a cage? - Pink Floyd.
    14. Re:As little as practically possible by Z00L00K · · Score: 1

      All logging will hurt performance, but how much depends on the system you are running, and for Java also the implementation of the JRE.

      I have been using Log4J under OpenVMS and that was a real performance killer in some places.

      But as a general, using all levels available will usually be a good thing. Even the 'fatal' level is sometimes useful. Like when you can't access a database then it may be considered as a fatal error that actually can be logged.

      --
      If builders built buildings the way programmers wrote programs, then the first woodpecker would destroy civilization.
    15. Re:As little as practically possible by zotz · · Score: 4, Funny

      I think you might be missing the main reason. If we don't reduce software logging, pretty soon there will be no old growth software left.

      all the best,

      drew

      --
      FreeMusicPush If you want to see more Free Music made, listen to Free
    16. Re:As little as practically possible by Bluesman · · Score: 1

      Games are the last bastion of efficiency. The reason they take so long to load is that they're massive today, and have to load huge textures and art into memory in order to run smoothly.

      But you're right, we really do blaze through memory and disk space like it's nobody's business, and it is a shame. I get the nagging feeling that stuff should just be faster a lot lately, especially since my new laptop has twice as much RAM as the hard drive space on the computer I used in college ten years ago.

      --
      If moderation could change anything, it would be illegal.
    17. Re:As little as practically possible by dubl-u · · Score: 2, Interesting

      I wonder why, given the huge increase in the performance of computers over the last ten years and more, why it sill takes some games one to five minutes to load.

      The goal of almost any commercial product isn't to be 100% awesome in every possible way. It's to be adequate on most measures and awesome in the few that buyers care most about.

      Game buyers care about volume and quality of content a lot more than load time. So even if some engineer figured out a way to cut load time in half, somebody would just add more content and bump up bit rates and quality levels until it was back to a tolerable load time.

    18. Re:As little as practically possible by the+eric+conspiracy · · Score: 1


      How can you demonstrate that you have fixed the reported bug if you can't recreate it in the first place?

      That is why I stated you have to log the process inputs.

      In any case reproducing the bug can really be a severe challenge because of the non-deterministic nature of modern software.

    19. Re:As little as practically possible by laejoh · · Score: 1

      Don't forget, next time you try to simulate the bug mount a scratch monkey first!

    20. Re:As little as practically possible by telbij · · Score: 1

      Assuming the behavior would be an improvement, which doesn't seem likely unless we breed a new class of super-developer and fire everyone who bullshitted their way through HR

    21. Re:As little as practically possible by FourDegreez · · Score: 2, Insightful

      Consider, the attitude of "just throwing more processing power" at something is driving up electricity usage at data centers and office buildings across the country. A responsible programmer in this day and age should consider the energy footprint their code will have. Yes, I am completely serious.

    22. Re:As little as practically possible by plover · · Score: 1

      Mod parent way up.

      We have a computer rack installed at each of our many satellite locations, and a main data center. The satellite racks are now filled with individual application servers and networking gear, and the tiny closets where they are installed are maxed out as far as energy consumption and cooling. We are fast approaching physical limits for how much code we can afford to add to these remote sites, and inefficient code makes the problem worse.

      --
      John
    23. Re:As little as practically possible by Nefarious+Wheel · · Score: 1

      As the PHB in Dilbert once said, "I want hourly reports on why we're behind schedule!"

      More aphorisms:

      o The way to herd cats is to put them in the vicinity of mice and let them do what they do best;

      o Too frequent demands for reports from software developers is akin to digging up a seed every day to see if it sprouted.

      These are the two prime insights I have from having run teams of up to 70 developers over a career a few decades long. It ain't your usual management job, and it's not done that well remotely. You have to troll the aisles and pick up the exceptions.

      But logging is good, in moderation. And throwing away resources is always bad; inefficiencies always show up and are noticed. I remember one of my programmers dragging in a rather large library just to sort a list that never exceeded a half-dozen short strings. I was less than pleased.

      --
      Do not mock my vision of impractical footwear
    24. Re:As little as practically possible by hwfa · · Score: 1

      I've written my own similar schemes in the past but in all cases I made sure the log-level required was settable in a config file readable by the program. So starting with "no logging", if you encounter a problem you change the config file's log-level and send the program a signal or, in the worst case if the program can't (or wasn't designed to) handle signals, stop it and restart to force it to re-read the config file. This way you only log verbosely when you need to and you don't need to re-compile/re-link to turn logging on.

      --
      Time flies like an arrow; fruit flies like a banana.
  2. Jakarta Commons Logging by Anonymous Coward · · Score: 1, Informative

    I use Jakarta Commons Logging...

    1. Re:Jakarta Commons Logging by I+confirm+I'm+not+a · · Score: 1

      I use SLF4J (as a wrapper around Log4J, usually), and consider Commons Logging deprecated. This is a blog post from the author of Commons Logging:

      I'll come right out and admit it: commons-logging, at least in its initial form, was my fault...If you're building an application server, don't use commons-logging. If you're building a moderately large framework, don't use commons-logging. If however, like the Jakarta Commons project, you're building a tiny little component that you intend for other developers to embed in their applications and frameworks, and you believe that logging information might be useful to those clients, and you can't be sure what logging framework they're going to want to use, then commons-logging might be useful to you.

      Most of the time I'm *not* building a Commons-style component, so JCL's dependencies hinder more than help. SLF4J, however, is very light-weight and very useful. One feature I like is a built in String.format function that won't evaluate if logging is disabled.

      --
      This is where the serious fun begins.
    2. Re:Jakarta Commons Logging by ciggieposeur · · Score: 1

      One feature I like is a built in String.format function that won't evaluate if logging is disabled.

      That's what I love about Lisp: the ability to make a macro that defers evaluation for precisely this case.

  3. What do you want to achieve... by AaronLawrence · · Score: 5, Informative

    As usual, "it depends" on what you are trying to achieve. Nobody can give you a blanket recommendation. But I guess in general: the log files need to give you enough information, that can't be got in other ways, to solve any problem that comes up.

    We have a realtime product that goes all over the world and talks to hardware that we can't always get access to ourselves. Therefore, we sometimes must debug our code remotely. Obviously, logging is critical to this. We keep sometimes hundreds of MB of logs and have archiving rules and a tool for users to collect them. Every layer of the system keeps it's own logs, and all logs have timestamps to milliseconds.

    In our case we log all the data back and forth, and then every important decision the code makes. For example if it decides there is something wrong with incoming data, it must log that. Any action it decides to take must be logged. Any data that will be passed on to other layers/the outside world must be shown. Generally, whenever we forget to log some of this data we will later regret it ("why the hell is it ignoring that device state..."). We also log at startup, basically the whole system configuration so that we can reproduce it.

    Callstacks when there is an exception can be very useful. However, a lot of "errors" (at least in our case) are not exceptions but rather unexpected data or behaviour. We rarely have a crash and in state-based systems a callstack doesn't tell you much about what's going on. So a callstack is not useful for all situations.

    Other times, you just want logging to give you a clue where in the code it was so you can run up the debugger and step through it (you do know how to step through code in the debugger, right?). In that case, too much logging can just get in the way. It might be sufficient in a GUI or web app to say which screen/page and which button was clicked.

    You'd hope users could report this kind of details, but not always: if the user is working in another language, in another country, with two layers of helpdesk between you and them, and they are busy doing other things when the problem occurs and only call in the issue an hour later, and the helpdesk takes a week to report it to you - you may just get a vague or even misleading report that no-one can remember when you ask questions. In those cases log files may be all you have to go on.

    There is also a tradeoff between log detail and manageability. Besides the difficulty in reading long log files, having a lot of detail means maintaining a lot of extra code. It also means that log files can become unmanagably large. In our case those hundreds of MB of logs can be a huge problem for customers to send to us because they have low quality internet connections (small companies in Mexico for example).

    --
    For every expert, there is an equal and opposite expert. - Arthur C. Clarke
    1. Re:What do you want to achieve... by ericfitz · · Score: 5, Informative

      +1 for parent.

      If you want good logging, then define requirements for it, just as you would for any other feature of the program. You also need to define the audience for the log. The comments thread has focused on debug logging for developers (Linus "no debuggers" Torvalds would be proud) but there are a number of reasons why the users who are stuck^h^h^h blessed with your software might want logging. For instance:

      - audit trails (often required by organizational security requirements or regulatory requirements)
      - accounting/billing (you'd be amazed at the odd ways people come up with to bill for things)
      - health monitoring (the admin might not want to watch your program 24x7 to see if it is running; they might want to program automation to be alerted when it is not working properly)
      - troubleshooting (believe it or not, your software might actually break when running in the wild)

      Anyway, think about your use cases, and then think about what to instrument for each use case, and what to put in the events.

      For instance, if you want to make your daemon monitorable for health, then think about all its dependencies. Does it read config from a file? The file is a dependency. What happens if a value is invalid? Does it fail or use a default? If it fails, reading the value is a dependency. Need a network socket? Dependency. Connection to remote machine? Dependency (actually multiple- name resolution, network connectivity, authentication, app-level connectivity, etc.). After you've enumerated all your dependencies, then add instrumentation in your code to log events when the dependency is unsatisfied (==unhealthy/broken), and when it is satisfied (==healthy). Make sure to log BOTH states, so that the monitoring app can decide which state you are in. Make sure to log only once per state transition. In each event, try to put as much information about the situation as you can- why you are in the state ("the value foo from daemon-config was invalid"), status codes, etc.- give your user a fighting chance of being able to use your log to diagnose and resolve the issue.

      If you want to instrument for audit, then I suggest reading the Orange Book or the Common Criteria documents for suggestions on what needs to be audited and what information to put in the events.

      For accounting, examine the RADIUS RFCs.

      Hope this helps.

    2. Re:What do you want to achieve... by linear+a · · Score: 1

      Amount of logging to do totally depends on the context. Regarding logging for regulatory/legal/sundry requirements, treat that as "user requirements" or, better, necessary features. Probably should decouple one's thinking of logging for those requirements from logging for development/improvement/troubleshooting purposes.

    3. Re:What do you want to achieve... by Machtyn · · Score: 1

      And for the love of Pete (whomever he may be) be consistent. If your program archives your log, make sure to use a consistent naming convention, and one that makes sense, throughout all the log files.

      Make sure you date/time stamps are consistent. If you are going to AM/PM designations, do this for all logs. 24-hour timestamp, all logs! Using an mm-dd-yyyy, mm-dd-yy, or yyyy-mm-dd style of date stamping, use one and don't vary.

      Being a young whippersnapper in the field, I wasn't sure if I should have written up a bug report on the issue. But once I started log browsing the files I realized how bad of a shape they were in (some log files even use multiple date-time stamping formats from line to line). YES, this was worthy of a ticket in the bug-tracking system (actually, several of them for each component).

    4. Re:What do you want to achieve... by jhol13 · · Score: 1

      Aspect oriented programming would seem to solve quite a few of your requirements.

      Unfortunately I have not (yet?) had time to evaluate AspectJ to know how it really performs.

    5. Re:What do you want to achieve... by Dr.+Winston+O'Boogie · · Score: 1

      "Depends" is absolutely the right answer here, as it is for just about every software decision.

      The log4j/log4perl/log4etc mechanism will serve the majority of needs, but not all of them. However, you should always start there unless you have a very good reason not to. You'll soon enough find out if you need something different.

    6. Re:What do you want to achieve... by Miss+Sing+Link · · Score: 1

      Good points, all.

      The reality is that there are a variety of debugging techniques that can be employed for each flavor and etiology of bug -- the major categories of which are: using external debuggers, instrumentation for logging, instrumentation for tracing (which is different -- another matter), automated analysis, and (at the bottom of the food chain) dump generation and code inspection. Each to its place and time.

      Discussions about debugging and instrumentation techniques always seem to get doctrinaire -- witness xero314's shouting "DO NOT" re: logging and pushing a fad paradigm. Linus's eschewing of debuggers is as religious a stance as any, when, in fact, there are many times when using a debugger cuts to the chase in shortest order, as well as many times where single-stepping is useless.

      Regarding logging, I agree: define your use cases, prototypical failure modes, and what your information extraction goals for instrumentation are, and design your logging scheme accordingly. My 25-year experience with this problem has shown at least the following as being highly desirable characteristics an any logging scheme, though:
        * Detail level: Selecting the degree of verbosity at the point of generation of each log entry is essential; post-filtering can compensate for sheer volume, but choking this at the source is much preferable.
        * Context: Generation of each log entry with a "component" tag (defined application-specifically) allows generation or filtration to be selected based on architectural scope as well, regardless of detail level. Why emit log entries whatsoever for the GUI when it's the core processing engine you're debugging?
        * Uniformity: Consistent formatting of log output vastly simplifies visual comprehension and creation of ad-hoc log postprocessing/analysis utilities. (echoing Machtyn)
        * Excisable: Implementing the logging scheme such that the code can be rebuilt sans the logging facility is especially important for performance-critical applications.
        * Efficiency: For logging high-speed events, there may also be a need to buffer log entries in RAM and emit those periodically in bulk to the log capture facility, depending upon the throughput of that interface. This to avoid entry into a Heisenbergian realm of your logging facility affecting core application functionality. In some extremely high-speed/-volume cases, you can log very terse event representations to an standalone log server box which does contextual lookup and expansion externally.

    7. Re:What do you want to achieve... by plover · · Score: 1

      While I agree with just about everything you mentioned, I would separate out audit trails and accounting/billing from the same class of logging as health monitoring and troubleshooting. Accounting and billing are functional requirements (and auditing is close behind that, especially with regulatory compliance,) whereas health monitoring and troubleshooting are non-functional requirements. Accounting and billing typically have their own specs, and really are deserving of their own feed. They shouldn't be mixed in with the support requirements for identifying problems, or developer requirements for determining root causes and fixing the bugs.

      I'm not saying that your troubleshooters won't be looking at the accounting data, but that the accountants will have no need for the typical "Created BillingObject / Destroying BillingObject" kind of logging.

      --
      John
    8. Re:What do you want to achieve... by Anonymous Coward · · Score: 0

      AspectJ compiles in the log statements, so there's no runtime hit above that involved in any logging.

  4. Three levels by spaceyhackerlady · · Score: 2, Interesting

    I find I usually end up with 3 levels of logging:

    Normal operation, often with some notion of "Yes, I'm still running even though I haven't done anything else lately".

    Details. Usually corresponding to processing steps.

    Algorithm tracing. This includes things like logging SQL queries. This is usually only of interest to me.

    ...laura

  5. I never really thought about it. by Ant+P. · · Score: 1

    After a few greps over the PHP code I'm working on at the moment, it turns out there's... no obvious log pattern at all. It's logging to the webserver error log in some places, logging to syslog in others, and using assert() randomly. Maybe I should've paid more attention to that bit while writing it.

  6. Standard format for domain information by MichaelSmith · · Score: 4, Informative

    I work on a large air traffic control system. Logging is a huge issue. Log files are collected centrally by a separate application. One important issue IMO is making the contents of your various log files meaningful to people who are not familiar with them.

    If your system has objects of type A B and C which can be handled by different components of your system then your should make the logging system in those components print information about those objects in exactly the same way.

    While you are at it, make the log format easily parsable by software. You don't want to be looking for a needle in a gigabyte size haystack of trace information without help from a tool which understands what it is looking for.

    1. Re:Standard format for domain information by TooMuchToDo · · Score: 1

      Enjoy all the fun of ADS-B =) As an IT professional and a private pilot, I hope if you're working on a project related to that, it works flawlessly.

    2. Re:Standard format for domain information by MichaelSmith · · Score: 2, Funny

      Enjoy all the fun of ADS-B =) As an IT professional and a private pilot, I hope if you're working on a project related to that, it works flawlessly.

      Cripes if it is working flawlessly we had better stop changing stuff ;)

  7. Log levels by Anonymous Coward · · Score: 0

    Use different log levels for events, e.g debug, info, warning, error, fatal ...
    This way the user can set a log level threshold above which messages will be ignored, so you don't have to plough through log files full of debug data, for example.

  8. It varies by pjwhite · · Score: 2, Interesting

    I will add lots of logging to debug a specific problem and then rip it out when the problem is fixed. Permanent logging includes run time problems like serial communication errors, file not found, etc. I like to make various logging functions switchable, so user input can be logged for example, but only when needed. Once a program is running well, it should only log data for dire exceptions, unless regular accounting logs are needed.

    1. Re:It varies by Zadaz · · Score: 1

      Yeah, it does really vary. I admit to doing this myself, though when I'm coding with others they despise me for crufting up the code. (Though coding with others often leads to conditions where logs are needed more often anyway...)

      During development and testing I log all non looping function calls, object creation/destroy and memory management, though I cull it as development proceeds (or just switch it all off).

      This is overkill but I got in the habit when maintaining someone else's code on a Project of Madness. At worst it helps me track down quickly where to start the debugger.

      Virtually always log all of my IO, keyboard, network, etc during development. There are some excellent tools out there (particularly used in game development) that can record and play back all IO to get you back to a bug.

      Don't overdo it, especially of you're logging over a network. I once worked on a net-based video player (think YouTube with video editing). The CEO took it upon himself late one night (honsetly) to add a lot more logging, not just errors, but status of everything, how long things took to load, including creepy ad tracking-type info like what buttons were most popular, etc, etc. Almost immediately the servers which were made to handle tons of bandwidth were getting hit hard by the clients. The logs where hilarious to watch as the clients started reporting "sever taking too long to respond, retrying" errors which quickly escalated into a DOS attack. I moved on from the company soon after, they are now defunct.

      It depends on the project, environment, and your style and skill where the sweet spot it.

  9. Logging to a database by Animats · · Score: 3, Informative

    My online applications log to a database, not a text file. Multiple applications on different machines can log to the same database table. There's no need for "log rotation"; old entries can be summarized and purged by date on the live database. With appropriate indexed fields, you can find key log entries in huge log files very rapidly.

    Even program faults are logged to the database. If the program crashes, the top-level exception handler catches the event, does a traceback, opens a fresh database connection, and logs the traceback.

    1. Re:Logging to a database by ericfitz · · Score: 1

      Logging to a database is generally a poor strategy. I see this over and over and cringe every time.

      Databases generally increase the overhead of logging significantly, and they don't add significant value. Sure, you can "select * from ... where ...". But do you REALLY need this? Most of the time when you need something from the log you can just grep /error/ or something comparable.

      Databases are great for reporting but are just unnecessary overhead for logging.

    2. Re:Logging to a database by plierhead · · Score: 2, Interesting

      IMO database logging has good points and bad points: On the good side, its easy to manipulate (query, purge, transform, summarise) the log entries. Also you can access the log entries remotely using the database tools you already know. On the bad side, its undoubtedly slower and more resource-intensive. Also, unless you have multiple DB connections (which itself raises complexity and overhead), then committing a log entry to the database will also commit your unit of work. It seems to work well for "user logging", i.e. where the end user of your application (rather than just the dev team) would want to read the messages.

      --

      [x] auto-moderate all posts by this user as insightful

    3. Re:Logging to a database by AaronLawrence · · Score: 3, Insightful

      Not to mention the added complexity and failure modes. All but the most trivial databases can go wrong in interesting ways, and when that happens where will you put your logging? It's precisely when things go wrong that you need logging the most. So you want the least possible dependencies. Right now, that's appending text to a file - file systems are simpler and tested more thoroughly than even the best databases can be.

      Like the user (or the system, or the virus...) shutdown the database server in the middle of operation. How do you prove that after the fact if the logs were going into the database?

      --
      For every expert, there is an equal and opposite expert. - Arthur C. Clarke
    4. Re:Logging to a database by marxmarv · · Score: 1

      What if your web server and programming language are forbidden from creating or writing files? (I am easily persuaded that this is a good idea from a system security point of view if you're a hosting provider.)

      --
      /. -- the Free Republic of technology.
    5. Re:Logging to a database by bpkiwi · · Score: 1

      Logging to a disk file might work when your application is running on a single box, but when you have something that runs on a pool of a hundred servers distributed across three different data centres, and you get a bug report that customer x "was using the service just after lunch yesterday and it did something funny", you're going to have fun trying to find the log file.

    6. Re:Logging to a database by FishWithAHammer · · Score: 1

      It's not a benefit to system security; just run the script engine (PHP, whatever) as the user who owns the account.

      --
      "You can either have software quality or you can have pointer arithmetic, but you cannot have both at the same time."
    7. Re:Logging to a database by Anonymous Coward · · Score: 0

      The worse problem of database logging is that it can't log anything while the database is offline or unavailable.

    8. Re:Logging to a database by CastrTroy · · Score: 1

      What kind of hosting provider are you talking about? Every hosting provider from the $4/month plans all the way up let you write to files in your own personal directory. Most that I've seen give you SSH access to your directory so you can do whatever you want. Come to think of it, I've never seen a hosting environment in any situation where you could write server side code, and couldn't write to a file on that server.

      --

      Anthropic principle: We see the universe the way it is because if it were different we would not be here to see it.
    9. Re:Logging to a database by AaronLawrence · · Score: 1

      Yep, large-scale, multi-user/multi-instance server applications have to do something smarter. I don't have any direct experience but it's obvious the file only approach is not very managable.

      Still the general advice applies: treat logging as any other development task. What are the requirements and constraints? In such a server environment you will arrive at different answers than for a single user desktop application or an embedded controller.

      --
      For every expert, there is an equal and opposite expert. - Arthur C. Clarke
    10. Re:Logging to a database by linear+a · · Score: 1

      Not necessarily. The worst problems I've seen with database logging were when the database becomes "uncleanly" unavailable and the application doesn't recognize what is happening. Ugh.

    11. Re:Logging to a database by fishbowl · · Score: 1

      >What if your web server and programming language are forbidden from creating or writing files?

      The time-tested solution in the UNIX world is log message over sockets to syslog daemons (on dedicated hosts, if the situation dictates).

      --
      -fb Everything not expressly forbidden is now mandatory.
    12. Re:Logging to a database by Anonymous Coward · · Score: 0

      Characterizing and profiling of the data being processed would be suited for a database, but not debugging.

    13. Re:Logging to a database by Anonymous Coward · · Score: 0

      Erm... where do you log database connection errors? :)

    14. Re:Logging to a database by Vulcann · · Score: 1

      Slow. Dog Slow! We also wanted to log to a database and thought the fastest way to go about doing it was a embedded SQLite database to do it. Apparently for simple operations (like writing log entries), its pretty fast. Even then, we found to our disappointment that writing to a flat file was at least 7-8 times faster (even when SQLite was writing in async). So if you have an app that does a lot of writes to logs, the db logger will slow it down.

    15. Re:Logging to a database by Anonymous Coward · · Score: 0

      Personally I log out to a simple standardized csv style format, then whenever I need to check the logs I just import the csv log file into the database and can query the values directly. It saves on having the increased dependency of databases at log time, while still giving you all the gains a database otherwise would afford you.

    16. Re:Logging to a database by Anonymous Coward · · Score: 0

      logging to a DB is idiotic - what if it's down?

    17. Re:Logging to a database by Kent+Recal · · Score: 1

      Logging directly to a RDBMS (I guess that's what grandparent suggested) is a bad idea in performance critical apps.
      We generally stream our logs over the net to a central logserver that writes them to files. The idea is to "fire and forget", get rid of the stuff as quick as possible so the app can proceed doing whatever it's supposed to do. Never block on a logging call.

      For thorough analysis we may *later* import the logfiles into a RDBMS. This can comfortably be done offline, no need to bog down mission-critical infrastructure...

    18. Re:Logging to a database by codepunk · · Score: 1

      Actually there is a very good solution for the situation you describe...

      --


      Got Code?
    19. Re:Logging to a database by pathological+liar · · Score: 1

      Like the user (or the system, or the virus...) shutdown the database server in the middle of operation. How do you prove that after the fact if the logs were going into the database?

      Er, check the database log. I would have thought that was obvious.

    20. Re:Logging to a database by amn108 · · Score: 1

      Irrelevant. And incorrect.

      With all due respect..

      Just because you cringe everytime you see it, doesn't mean you figured it all out.

      Databases are created to store data and data relations. Database servers strive to function to rapidly update databases in parallel with other system services, achieving great speeds, especially when client and server are distributed over network. The number of advantages is obvious, even more so when the whole testing is on Gigabit LANs, where one can send the log packet to a nearby database server with a single UDP Berkeley API call, an analogy with Fire-And-Forget missile launch. Logs can later be retrieved and analyzed without doing homecooked decoding and funky string parsing, and even without the client machine service.

      What is poor STRATEGY is instead clogging diskspace with plaintext home-brewed logs, that noone else understands (only people firing up text-editors). This wastes CPU, system bus and the harddrive. This aint MS-DOS no more, mind you.

      The ONLY reason one does not REALLY need it, as you put it, is when one wants to reinvent the wheel with style.

      Granted, the problem with my proposal, and the reason you are asking "Do you REALLY need this?" is because we are afraid to burden client systems with uniform system-wide industry-grade logging interfaces and implementations, so that both Solitaire (god forbid) and the latest Linux enterprise application benemoth MyApp may use the same shared logging facility.

      Right now Windows only has the Event Reporting Service API (with few others prolly, given the rate MS is jumping APIs all the time) and AFAIK Linux uses the (much better longterm- wise) pipe logs on filesystem (also with few other options being available, I am sure). The former is just too much overhead for tracing, even though it is called Event Reporting, it only is able to keep with such reporting in case of really BIG events, not like "variable i incremented" type of events.

      Even the very filesystems we use to distribute and store our logs with, are moving in the direction to become database driven. Which means that people thinking "oh, let me just invent my little log, and store it plaintext to disk" will be doing themselves double disservice, by designing applications that in a few years from now will look archaic, because they do logging like composing the log string then issuing a file write call, which invokes a database driven fs that allocates a single column tuplet table and stores the entire complex string to it. Could work, but looks sort of useless.

      Please be careful giving advice like that, everything that is put into the world is used by real people. Not just made by programmers, that do not REALLY need this or that.

      I am out.

    21. Re:Logging to a database by marxmarv · · Score: 1

      If the httpd user can modify the content root, you're begging for trouble. If the httpd user can modify no file on the system, a lot of exploits become that much harder to execute.

      --
      /. -- the Free Republic of technology.
  10. taking it a big further - no logging at all by JonTurner · · Score: 3, Insightful

    Don't coddle weak programmers... it's survival of the fittest out here. Either they learn to nourish themselves from the ample teat of a stack dump, or they must perish. It is for the good of our civilization. I know this seems harsh, young Jedi, but it is the way of the Elders of Assembler, from the ancient Time Before OO. Now go Forth and code.

    Okay, joking aside. Parent has a great point -- logging can generate incredible volumes of text and can form a remarkable bottleneck, especially on VM systems where your OS may not be the only one hitting the disk.
    So take advantage of Log4J/Net's ability to log at different severity levels and make logging globally configurable so you can enable/disable entirely at runtime. I'd recommend you log the following : object creation, scarce resource allocation, recoverable failure/error conditions and unrecoverable failures. Preface each severity level with a unique label so you can grep for it later. Even at the most verbose level, you can then grep your output to see only what's of interest to you (e.g. "unrecoverable:...").

    1. Re:taking it a big further - no logging at all by a_claudiu · · Score: 1

      I develop java web based enterprise applications that are heavily dependent on external applications (external means outside our team). Log4j is gold but I can give some tricks:

      1. Log as most of possible but do not forget
      if(LOG.isDebugEnabled()) {
      LOG.debug(...)
      }
      In an enterprise application CPU is (should be) not the bottleneck but file IO, DB's and Network, so a simple if does not hurt your performance at all.

      2. Always log request response to external services on info level (the default level I'm using in production). Most of the problems are not yours and you should be able to blame the others as quick as possible :).

      3. Create a special page that takes 2 parameters loggerName and loggingLevel that can change the logging level at runtime Logger.getLogger(loggerName).setLevel(Level.toLevel(loggingLevel)). This way you can change the logging level at runtime and have extra information in case of problems.

      4. At every request put the authenticated user id in threadlocal and create a custom Layout extending org.apache.log4j.PatternLayout that is overriding format(LoggingEvent event) with
      return user from thread local+"|"+super.format(event);
      This helps you tracing what a specific used did. Also log the http request parameters (they are not so many unless you are regularly uploading files)

      5. Consider AspectJ for logging function calls, profiling and unit testing. I do not like AspectJ except for these cases.
      AspectJ makes your program harder to understand and maintain, so the rule of thumb should be: your application should run the same with or without aspects enabled, DO NOT USE AspectJ for anything like managing transactions or anything that is business related unless you are thinking about some job protection.

    2. Re:taking it a big further - no logging at all by BobGregg · · Score: 4, Interesting

      I just had occasion to try and optimize an old C++ app that was having performance issues in certain sections. This app had its own custom logging system using Unix message queues, and was always a little suspect. I noticed as I tried to instrument the code that whenever I seemed to get close to the bottleneck, the problem seemed to move to a different part of the code. Finally, it dawned on me that the log system itself was part of the problem. In fact, the log object constructor was taking significant time, including (in some places) within critical (as in lock/unlock) sections. Most critical classes had been defined with an instance of the logger as a member, which was overkill. Doing nothing more than changing the definitions of the log objects to static dropped critical section performance by an entire order of magnitude.

      Moral: Do logging, but do it judiciously. Know what you're doing - and know what the log system is doing too.

    3. Re:taking it a big further - no logging at all by Beezlebub33 · · Score: 1

      1. Log as most of possible but do not forget
      if(LOG.isDebugEnabled()) {
            LOG.debug(...)
      }
      In an enterprise application CPU is (should be) not the bottleneck but file IO, DB's and Network, so a simple if does not hurt your performance at all.

      I have not found this to be particularly useful. The only time that this is beneficial is when the parameter being passed in the debug call includes something difficult or time consuming to create. Rarely is this the case. When it _is_ the case, you should use it, but otherwise, conserve your brackets.

      I'd add:
      6. When you throw an exception, log it.

      --
      The more people I meet, the better I like my dog.
  11. Not all logging is useful ... by Eric+Damron · · Score: 1

    I set a "logging level" in my configuration file so that when I need a program log to be verbose I can make it so but for normal use I can keep the logs fairly light.

    A log is only useful if it records information that is used. The problem is that sometimes everything is running fine and super verbose logging is a waste but then something in the environment changes and suddenly temporarily logging a lot of debugging information is useful. Setting a "logging level" in a configuration file lets me do light logging, verbose logging and just about anything between without having to recompile my projects.

    --
    The race isn't always to the swift... but that's the way to bet!
  12. whatever you do, don't use nfs by Sir_Real · · Score: 4, Interesting

    If you're using log4j, don't use multiple hosts to write to the same nfs filesystem file. You'll run into blocking issues and log4j doesn't handle them correctly. The nirvana of clustered app logging is an async JMS queue. You fire off the message and forget it. You don't wait for file handles.

    1. Re:whatever you do, don't use nfs by TooMuchToDo · · Score: 1
      Holy shiat. I just thought of a kick ass way to use the messaging queue system Amazon's cloud computing provides.

      I'm off to prototype some code. Thanks!

    2. Re:whatever you do, don't use nfs by Anonymous Coward · · Score: 0

      An interesting idea, but in which ways would this be superior to a SyslogNG-based solution? It appears as if it would be a lot harder to set up. Genuine question!

    3. Re:whatever you do, don't use nfs by hackingbear · · Score: 1

      log4 has all sorts of destination adapters, including JMS.

  13. not cpu bound... disk bound by JonTurner · · Score: 5, Insightful

    It's not CPU that's at a premium, it's disk IO. And on virtualized machines (such as is extremely popular in corporations and hosting farms) where there might be four different OSs running on the same physical hardware, disk becomes a scarce resource very, very quickly. And not only does your virtualized server go to shit, it takes the others down with it since they can't get timely disk access, either.

    1. Re:not cpu bound... disk bound by 0123456 · · Score: 1

      "It's not CPU that's at a premium, it's disk IO."

      True, but again, if it's an important system you can buy a dedicated server or a second disk for logging for the cost of a few hours (possibly a few minutes) of downtime.

    2. Re:not cpu bound... disk bound by Heembo · · Score: 4, Informative

      For high availability clustered web applications, it's not disc IO that is the problem, but network overhead.

      I tend to use log4j and asynchronous logging that passes log messages to a syslog server that can handle the file io - and it ends up being network overhead that is the killer.

      --
      Horns are really just a broken halo.
    3. Re:not cpu bound... disk bound by hackstraw · · Score: 3, Insightful

      I tend to use log4j and asynchronous logging that passes log messages to a syslog server that can handle the file io - and it ends up being network overhead that is the killer.

      People have said disk io, CPUs, and they say they are both cheap. NICs are VERY cheap.

    4. Re:not cpu bound... disk bound by morgan_greywolf · · Score: 2, Interesting

      Companies with virtualized machines are often also using storage area networking and related high-availability technologies. The traditional bottleneck associated with disk I/O does not happen nearly as badly.

    5. Re:not cpu bound... disk bound by Gazzonyx · · Score: 1

      But PCI slots aren't in a 1U.

      --

      If I mod you up, it doesn't necessarily mean I agree with what you've said, sorry.

    6. Re:not cpu bound... disk bound by gallwapa · · Score: 3, Informative

      ehhh. HP 373i quad port gig nic pci express kicks ass and only runs about $350

    7. Re:not cpu bound... disk bound by ralfe · · Score: 2, Interesting

      One solution I have found for this is to write my own logging application. I write mainly web-based software and AGI stuff, and all my software makes use of the same logging application. Basically what happens is that when I want to write to the log (which is stored in a MySQL database so analysis and viewing the logs is enhanced), the software simply tells the OS to execute the logging utility which can be niced or deprioritised on the system. This way, the software doesn't have to worry about I/O to the log, it doesn't have to wait for MySQL and it can carry on without the hassles involved in rewritting bits of logging code for each project. I have found this works really well for me, and having all the logs for all my code in a central database makes it easy for me to write some nice little AJAX/PHP app to apply filters to the logs or to analyse the logs, whatever I want basically. However, I know this method is not practical for many situations.

    8. Re:not cpu bound... disk bound by WebManWalking · · Score: 1

      10-4 on the importance of monitoring disk I/O. At my site, we call log4j to log the elapsed time of every I/O at the INFO level, regardless of whether it's generated SQL or stored procedures of SQL that never changes. The overhead of no-op calls to log4j is on the order of microseconds when log4j is turned off, so there's really no reason not to. One unanticipated benefit is that we can turn on logging when an application crashes and look at the logs to see how far it got before it crashed. This points us to the vicinity of where the error occurred. Of course, we also log the elapsed time to generate every Web page to detect poorly written code.

    9. Re:not cpu bound... disk bound by leenks · · Score: 1

      Maybe they are, but network infrastructure isn't necessarily. It can be very difficult and/or expensive to get suitable connectivity between different racks in a data center your corporation owns, let alone one that you are merely hiring space in.

    10. Re:not cpu bound... disk bound by Anonymous Coward · · Score: 0

      NICs are cheap. But adding more NICs doesn't give you more performance, does it. You have to increase the speed of the whole network (which is limited to at most 1 GBps these days).

    11. Re:not cpu bound... disk bound by sgbett · · Score: 1

      interesting post, no mod points though. is it closed source? I'd be interested in looking at something like that.

      --
      Invaders must die
    12. Re:not cpu bound... disk bound by Architect_sasyr · · Score: 1

      Perhaps they become worth it in this sort of environment though. A Fibre channel is probably the best bet though. It becomes a trade off between what you want to spend and what you really need.

      --
      Me failed English...
      FreeBSD over Linux. If my comments seem odd, this may explain...
  14. Depends on the application you're writing by lteo_calyptix · · Score: 2, Interesting

    It depends on the app you're writing -- is it a web app, a database app, a userspace program written in C, a Perl/Ruby script, or.. At work we created our own logging library in C to emit log events for different levels, e.g. informational, debugging, warnings, errors, fatal messages. We then have wrappers around that library so that languages like Ruby can access that logging library. But on hindsight I think I would've just used syslog if I had to start over. :)

  15. Production or Dev Environment? by Comatose51 · · Score: 1

    Are we talking about production environment or a development environment? In production, we log our exceptions or when major changes happen, such as something being deleted. The goal there is to help our support team and customers nail down the problems when they arise. I don't know if LoC is the right measure for these things. Our UI (it's a web app) has little to no logging since errors just appear when the UI has a problem. So we can have hundreds of lines of code with no logging there. Our backend logs much more frequently. Basically every "catch" block has a call to the logger (log4net is what we use as well). Still, most of us prefer to hook a debugger up to the process during development and rarely ever go back through the logs. The only time the logs are frequently used prior to production is when we're running automated QA and we want to know what happened when a test case fails. In that case, when we compile with the debug flag, we log quite a bit and more frequently and log things that only our developers would understand.

    I guess maybe the rule of thumb is, depending on the environment, generate logs that are targeted at the audience when things go wrong. I'm not convinced frequency of logs is a meaningful measure of appropriate logging.

    --
    EvilCON - Made Famous by /.
  16. youre doing it wrong by Tennguin · · Score: 1

    "I personally find that writing debug and informational messages about every 2 to 5 lines works well for debugging an issue" Learn to use a debugger. What you are doing is backwards. The logger is meant to help out when the application you are working on is being executed remotly and attaching a debugger isnt practical or desirable. There is no code in the world that I can think of that needs a log line after every two steps in a procedure.

    1. Re:youre doing it wrong by wdsci · · Score: 2, Interesting

      Agreed, logging every 2 to 5 lines gives you the kind of information that you should really be getting with a debugger. Of course, when you're trying to diagnose a specific problem, sometimes it can be easier to put log messages every line or two than to repeatedly step through the code with a debugger, but that's sort of the same thing, just a temporary debugging aid - most of that logging output should be removed once you've figured out what's going on. For general use, I think about one log call per function might be reasonable - more if it's a long function, or none if it's a short function that does something really simple. And even most of those should probably be disabled once you release the software.

    2. Re:youre doing it wrong by Anonymous Coward · · Score: 0

      Or... It is a fault that happens every oh 10000 cycles of the system or so... at unpredictable times, in a way that should not be possible without some outside factor.

      Then 'excessive' logging can be quite useful. Special cases require special and often technically "stupid" solutions sometimes.. ;)

      An example:

      An RFID based system I worked on would sometimes get garbled data back from an external embedded device. This data would check out through CRC etc but the actual data was wrong. This happened with a few rfid-cards out of some tens of thousands.
      Logging what is received and sent to the embedded device for every read was hugely useful in tracking down the issue.
      Turns out there was a bug in a conversion routine on the embedded device that only surfaced with a specific pattern in the SN on the rfid-tag.. slipped through testing somehow.. not my project ;)

      The logging here generated about 10 lines per read which was a good amount of data with several thousand reads a day but it was a hell of a lot better than what management suggested... That I sit watching the system screen and wait for it to bomb :-p

    3. Re:youre doing it wrong by ciggieposeur · · Score: 2, Interesting

      There is no code in the world that I can think of that needs a log line after every two steps in a procedure.

      Any code in which timeouts can affect the result would require this kind of logging, which includes networking code or code that handshakes between multiple threads/processors. Example: debugging something like a new x/y/zmodem implementation is nigh impossible within a debugger because your side must respond within 10 seconds or the other side will start acting differently.

    4. Re:youre doing it wrong by Simon80 · · Score: 1

      In that case, you were logging in response to a specific problem, and it wouldn't have taking a log line every 2-5 lines of other code in order to achieve what you wanted. If code needs to log that often, it's probably lacking in proper use of abstraction.

    5. Re:youre doing it wrong by toddestan · · Score: 1

      There is no code in the world that I can think of that needs a log line after every two steps in a procedure.

      I've had to do stuff like that when the software interfaces with hardware (stepper motors, things like that) that's doing stuff, and timing is critical. In other words, you can't just pause the software in a debugger to examine things, because then the hardware will get itself in a bad state and you can't continue the software where you left off.

  17. I Don't! by Vectronic · · Score: 4, Funny

    I don't, save the rain forest, hug a tree, prevent deforestation, stop logging now!

    1. Re:I Don't! by Shados · · Score: 0

      Text files fortunately do not use paper.

    2. Re:I Don't! by robfoo · · Score: 2

      *whoosh*

    3. Re:I Don't! by Shados · · Score: 2

      Oh...hrm...yeah, I feel dumb.

  18. I let the kernel do it for me by ILongForDarkness · · Score: 2, Funny

    Segmentation fault: core dumped

    1. Re:I let the kernel do it for me by SloWave · · Score: 1

      And don't forget to 'ulimit -c 0' to increase efficiency beforehand.

    2. Re:I let the kernel do it for me by ILongForDarkness · · Score: 1

      Yeah good point. Who really looks at the core files anyways?

  19. Logging, parts I and II by trapezoid · · Score: 2, Informative

    I wrote up a two-part piece on logging best and worst practices a while back. See Part I and Part II if you are interested.

  20. TOO MUCH! by imp · · Score: 2, Interesting

    1 line of logging per 200 lines of code is way too much. 2 in 5 lines is absolutely insane. I've seen way too many systems where the logging made it totally unusable. People just don't realize the costs of logging everything.

    There's absolutely no way to document anything this verbose. Without documentation, the logging is useless.

    1. Re:TOO MUCH! by ciggieposeur · · Score: 1

      People just don't realize the costs of logging everything.

      Are you operating on embedded systems? Because anything faster than about 500MHz can easily do a few million "if (logging_level >= LOGGING_LEVEL_CONSTANT) { ... log_something ... }" type checks per second.

    2. Re:TOO MUCH! by droopycom · · Score: 1

      Whether your running an Embedded System or Big Iron does not really matter. What matter is how many cycles your logging is taking that could be used to do productive work, compared to the total cycles used for productive works.

    3. Re:TOO MUCH! by thogard · · Score: 1

      Teh big cost is dealing with the debug output. Thats why some programs have used category.level debugging for decades. An example would be sendmail's -d 1-99.99 shows you everything but -d 27.10 will show debug levels 1-10 of just the alias processing code.

    4. Re:TOO MUCH! by Anonymous Coward · · Score: 0

      Thats why you in most enterprise applications can turn the log serverity knob on and/off during runtime. A classic tradeoff between logfile size and how often you are bitten by a bug, solved by a way to turn on verbose logging when you need it.

    5. Re:TOO MUCH! by ciggieposeur · · Score: 1

      Of course it matters. If your entire logging infrastructure when turned off adds up to a whopping 0.1 seconds per hour of useful work, but can be turned on when needed to locate real problems, then not having it is pretty dumb.

    6. Re:TOO MUCH! by Kent+Recal · · Score: 1

      You have obviously never worked on a large scale system where latency matters.
      Conditional logging is not quite as trivial as you suggest because often the decision about "to log or not to log" is more complex than a simple branch. If your log-level granularity is "ON or OFF" then you will never be able to switch it on in production because the flood bogs down your application. If your granularity is finer than that then you face a bit of decision making (at least a hash lookup) for every log-statement. Yes, that's still cheap but by no means "free" as you suggest. It can make quite a difference under load.
      Finally you have to consider the case when logging is actually (partially) enabled. That's additional I/O your app has to perform which may or may not significantly change the load profile, depending on what your app does. Guarantee'ing that a log-call will never block can be non-trivial in a multithreaded app and establishing this guarantee comes at a cost of its own.

      Thus, your blanket assumption that any given "entire logging infrastructure" will only give or take "a whopping 0.1 CPU-seconds per hour" is... pretty dumb.

    7. Re:TOO MUCH! by ciggieposeur · · Score: 1

      Thus, your blanket assumption that any given "entire logging infrastructure" will only give or take "a whopping 0.1 CPU-seconds per hour" is... pretty dumb.

      I wasn't actually assuming that but I suppose you could see it that way. I actually said "IF your entire logging..."

      I must have been somehow spoiled by my logging infrastructure. We could specify log levels for individual subsystems (down to the class level) and unless logging was actually turned on for a particular class nothing was generated to be thrown away at a different layer. And it DIDN'T take a hash lookup on each logging statement (that would have been stupid indeed), instead logger flags were changed just once across the (clustered) application when the user changed the trace spec. This consumes only a couple MB more memory having all those separate logging flags, but performance was fine even with a static synchronized output function -- though it helped that it cached the string representation of System.currentTimeMillis(). And yes, enabling a lot of logging would slow things down considerably -- up to 99% slowdown in some cases -- but when logging was off there was essentially no measurable difference between having the check-then-log-statement and not having log-statements at all. (And yes, I had to verify that myself because of other performance bottlenecks being investigated.) Logging messages would appear in stdout/stderr and also in the appserver cluster console for severe errors.

      These days I wouldn't even bother with any logging framework that doesn't have at least these basic features.

    8. Re:TOO MUCH! by droopycom · · Score: 1

      So ? You didnt get my point...

      It still doesnt matter if its an Embedded System or a Big Irong server.

      My point is your number (0.1 seconds per hour of useful work) does not only depend of your platform but mostly of your application.

      That is: a Specific Application on a Embedded System might had 0.1 seconds per hour of useful work. An big application on a SuperComputer might add 10 seconds per hour of useful work.

  21. I handle mine by TornCityVenz · · Score: 1

    with guru meditation errors of course!

    --
    I Need someone to rebuild a Digitech Digital Delay pedal for me....for me...for me...for me.
  22. Comment removed by account_deleted · · Score: 2, Interesting

    Comment removed based on user account deletion

  23. Logging is a form of debugging by Anonymous Coward · · Score: 0

    Debugging is for people who write buggy code. Nuff said.

  24. Try putting logging calls in the vital parts by Zapotek · · Score: 1

    I personally put logging calls in the most used functions.
    For example in my MySQL handler class I have put the logging code into the function that executes the SQL queries, which is the function that everything else relies on.
    So when I need to debug I have a stack of SQL queries along with their error codes and error strings.
    That's enough to figure out what went wrong.

    My point is put the logging calls in the vital functions, the ones that do all the work. This will save you many code lines and processing cycles.

    1. Re:Try putting logging calls in the vital parts by Firehed · · Score: 1

      That comes at the expense of a LOT of usually-unnecessary disk/network activity, as you're calling the thing ALL THE DAMN TIME. Just imagine if Facebook or Yahoo logged every query executed. Maybe it helps during the early debugging stages of smaller apps/sites, but your logs would get out of control faster than you can imagine on larger utilities... they'd need some sort of gigantic SAN just for the log file.

      Or did you mean that you only log failed queries? That would make a hell of a lot more sense.

      --
      How are sites slashdotted when nobody reads TFAs?
  25. Filters by zarthrag · · Score: 2, Interesting

    My logging is setup so I can quickly filter down to the type of data I want. It's more than just "information", "warning", and "error" - but by (cpp)file, module, etc. That way, if an issue arises, I can eliminate the cruft and see just what I need. Just takes planning.

    --
    Why can't all fpga/microcontroller manufacturers just release free optimizing compilers???
    1. Re:Filters by TwinkieStix · · Score: 1

      Logging by severity and by file/class/package is an inherent feature of log4j (what the OP was talking about).

  26. Syslog by Pr0Hak · · Score: 1

    Syslog, of course.

  27. Whatever is useful while programming. by Restil · · Score: 2, Informative

    I tend to do my debugging by inserting a lot of printf statements to indicate where in the program I currently am and the value of any critical variables at that time. As the output information is no longer needed (i.e. I fixed the bug it was attached to), I tend to cull out whatever isn't useful anymore. However, I tend to keep starting messages in function calls related to a routine I'm working on or making more than a trivial change to... since chances are, knowing me, I'm going to end up putting them back in there anyway once I create a new bug... and lets face it, it WILL happen.

    Once I'm done, I go back, remove or comment out (usually just comment out) all the messages that have no redeeming value for a properly functioning program, and turn the rest into debug statements which print based on the debug level provided at execution time... or sometimes I use a mask to select which types of messages to display.

    -Restil

    --
    Play with my webcams and lights here
    1. Re:Whatever is useful while programming. by Shados · · Score: 1

      I tend to do my debugging by inserting a lot of printf statements to indicate where in the program I currently am and the value of any critical variables at that time.

      Wouldn't using a...you know... -debugger- be more efficient at doing that? Breakpoints, watching variables, etc?

    2. Re:Whatever is useful while programming. by CastrTroy · · Score: 2, Insightful

      Disappointingly enough, this is one of the things that isn't covered very well in a lot of courses. I didn't get any exposure to debuggers in any classes I took throughout university. I learned about it myself. Same goes for a lot of other useful tools like source control systems. While I learned a lot while taking my degree, very little of what I learned dealt directly with the process of how you actually sit down and write code. Seriously, some people think that printf really is the best/only way to debug, and I can see why. My first Java course had us all typing up code in notepad and compiling/running from the command line. After that, courses just told us to use Java, without pointing to any specific tools that we should be using. It was so bad, that first year Java actually used a special add on library to do input output using a GUI, so when it came time do not use that in second year, we had to go figure out how to do IO all over again.

      --

      Anthropic principle: We see the universe the way it is because if it were different we would not be here to see it.
    3. Re:Whatever is useful while programming. by Zadaz · · Score: 1

      Wouldn't using a...you know... -debugger- be more efficient at doing that? Breakpoints, watching variables, etc?

      All you get on some embedded systems is serial output. No watch variables or breakpoints, just whatever text you can manage to dump out the serial port.

      In addition logging function calls can quickly narrow down -where- to put your breakpoint and what variables to watch.

    4. Re:Whatever is useful while programming. by T3Tech · · Score: 1

      While I could use gdb in a client/server fashion for the embedded stuff I do (linux based), I find it much simpler to just add debug level printf statements. Then, telnet in and run the software in debug output mode in the foreground to at least get an idea of where in the code the problem is.

      Of course, as stated in a sibling post even this isn't an option on some embedded systems.

      --
      Of course I didn't RTFA... why would I do that? You really are new here aren't you? Don't let my UID fool you.
    5. Re:Whatever is useful while programming. by Anonymous Coward · · Score: 0

      Haven't any of you heard of Test-Driven Development? You'll never use a debugger again!

    6. Re:Whatever is useful while programming. by isj · · Score: 1

      A debugger is not always feasible. I have worked on systems (and still am. sigh...) where there are 3 debuggers:
      Debugger A can run the program but cannot show symbols in shared libraries.
      Debugger B version X cannot run the program due to threads but can show a stack backtrace from a core dump.
      Debugger B version Y cannot run the program due to threads but can show values of variables in a core dump.

      So logging and printf are my friends.

    7. Re:Whatever is useful while programming. by pjt33 · · Score: 1

      When you write your test, write your code, and don't understand why your test still fails, what do you use instead of a debugger?

    8. Re:Whatever is useful while programming. by plover · · Score: 1

      I think that's how a lot of logging starts out, but you should learn from "the lazy programmer" lesson here. If you're going back to cull out the logging statements, comment stuff out, whatever, it's time to plan out a more advanced logging system.

      About eight years ago we got sick of the "go back and edit the printf()" philosophy, and created a set of macros that wrapped our tracing. The original design goal was to make our TRACE macro have the same syntax and flexibility as printf(), while giving us the ability to set detail levels and log errors. We ended up with a pretty simple logging API wrapped in a .h file. Keeping it simple was key to getting people to use it instead of printf(). Once we had been using that for a few years, we went back and replaced the guts of the tracing code with more efficient, controllable code. We added a dynamically loaded module that uses an in-memory trace queue with a threaded backend that does the actual disk I/O, it responds in real-time to changes in logging levels, does the log file maintenance, and in general performs fast in all kinds of situations. All while keeping to the same simple API as the original TRACE macro stuff, so everything is fully backward compatible.

      It only took a little while to create the original API, and it certainly took more time to replace the engine with a more efficient model. But the end results are consistent, controllable, high performance tracing. The static API is as thin as it can be to avoid recompiling other components. The dynamically loaded engine gets upgraded every so often as new requirements come along. And the API even allows the program to continue to run if the tracing engine can't be loaded, following the idea that application uptime is more important than tracing.

      Total cost? Less than the time we've saved fixing the bugs we've found with it. If we had to do it again, I'd rather use a common component such as log4xxx than to roll my own, but I'd still want to isolate it behind my own wrapper to meet our requirements. Actually, with our design, I could replace our tracing object with a log4xxx type component without changing our application code (but I'd still have to recompile.)

      --
      John
    9. Re:Whatever is useful while programming. by bledri · · Score: 1

      Your brain?

      Seriously, I'm a huge fan of debuggers but there are situations where they are not feasible. In those situations you use what you can to understand what's going on. That may be logging to a file or the console. Heck, I've even thrown in an intentional division by zero so I could get a quick stack trace dumped to the console.

      --
      Some privacy policy Slashdot.
    10. Re:Whatever is useful while programming. by pjt33 · · Score: 1
      Okay, I'm officially confused. You're clearly responding to my post, but your argument is with the grandparent of my post, not with mine. The flow as I read it goes:
      1. Shados: debugger vs println
      2. AC: use TDD and you don't need either
      3. Me: yes you do
      4. You: debugger vs println
    11. Re:Whatever is useful while programming. by bledri · · Score: 1

      Okay, I'm officially confused,

      Me too, I was responding to a completely different thread! I can't even find the one I was responding too, although I'm at work and can't look that hard.

      FYI - I was responding to a thread where someone asked what you could use to debug if you don't have a debugger. Hence my completely out-of-context 'Your Brain' quip. Man, I'm really perplexed as to how I could screw up using the Reply button. Hopefully I get it right this time...

      --
      Some privacy policy Slashdot.
  28. Log4j supports selective logging by coyote-san · · Score: 1

    Log4j supports selective logging. That means you can have info/debug/trace priority messages in place, but never see them in the log unless you explicitly enable extra logging for that class or package. You can do this at runtime, e.g., via something like 'chainsaw' (which attaches to a running process) or hooks in your UI.

    Our policy is that logs are usually very quiet. Application startup/shutdown and not much more. But if there's a problem the debugging messages are already in place to let us peak into the system, even if it's been deployed to a production site.

    BTW AOP is also great for this. You can configure logging interceptors that log activity when you're in a development environmnent, but easily removed in production. This is a natural approach when going from one layer to the next, e.g., when wrapping the DAO layer.

    --
    For every complex problem there is an answer that is clear, simple, and wrong. -- H L Mencken
  29. Lotsa logging by SpinyNorman · · Score: 3, Informative

    I write code for Telecom test systems that need to run 24x7 processing highly varying requests from dozens of different client systems. Our system consists of dozens of different processes/components per host, with multiple hosts all invoking components on each other as needed (all via CORBA). There are very many paths that any request can take through our system.

    In this environment we log VERY heavily since each request is close to unique and we need to be able to determine the path it took through the system, and why it did, and what happened, in the event of any bug report. Some of the haviest used modules can produce close to 1GB of log per day per host - upto a couple hundred lines of logging information per request per process that it passes thru. We use a custom printf-like log library written in C++ (that auto rotates the log files based on various criteria), a custom tail utility for dealing with the large log files (tail a log file from a given timestamp - done instantly via binary search on the timestamps) and a daily cron job to compress the older log files and move any older than 5 days off the production servers to someplace with more storage.

    1. Re:Lotsa logging by gbjbaanb · · Score: 1

      strangely enough, we do something similar - only I work in emergency response call centres.

      We don't do 1Gb of logs per day, only about half that...

      Some of the comments on this thread all seem to be about performance and code-level logging (ie what path the system is taking through the code). Our logging only does that incidentally, as the logs are vastly more important for tracing through why something happened the way it did. eg, recent reports from a customer was "at 2am one of the operators reported that unit xyz was recommended for a call when we'd expect unit abc should have gone", then we trawl the logs to see why (usually because the operator marks the unit as 'not-recommendable' and forgets to turn him back on - I consider that a bug in the software).

      Nobody cared how the system processed it, or the code path, but we do care about the state of data at the time.

  30. Forget logging: use an IDE, exceptions and asserts by dstates · · Score: 1

    First, a well functioning app should really not be generating any log file except what the application itself needs (e.g. usage logs for a web server).

    Second, instead of wading through reams of log output, use exceptions and assert statements so that you only generate log output when something has gone wrong.

    Finally, use your IDE. Instead of trying to infer state by combing through logs, set break points where you catch an exception. This lets you traceback to see why you ended up with the exception without suffering much if any of a performance hit.

    As we move to increasingly distributed systems, log files become less useful. For many distributed apps, you may not even have access to all of the various file systems where a log might be written. Best solution is to rely on highly modular and thoroughly debugged code.

    --
    Statesman
  31. One line of logging for every line of comments by sprior · · Score: 3, Funny

    That should be about right...

  32. Logging or debugging? by serviscope_minor · · Score: 1

    Sometimes with printf style debugging, so much data comes out that it is not practical to use the logging in normal code. That said, I dump all data I want to stdout, and use AWK to massage the results. If you put in some kind of tags or markers, this can help simplify the log processing code significantly.

    You don't need databases or anything structured, simply one stream per process. The process will run sequentially, so a sequential storage structure (ie a file) is the best matched to the data. Also, I avoid markups like XML and so on in favour of plain text, since it is easier to process the plain text with standard UNIX tools. They also tend to operate more efficiently since parsing is a matter of looking for whitespace or \n. Also, the same techniques work from any reasonable language, since they can all output text.

    --
    SJW n. One who posts facts.
  33. Um. Write to memory often, disk seldom and filter? by Anonymous Coward · · Score: 0

    You have to be logging a *lot* to dog down a system if you're writing to memory and dumping out to disk every 100000 lines or so (or on exit). As for viewing, how hard is it to filter written output in place?

    Did I miss something?

  34. Bleh by Anonymous Coward · · Score: 0

    Logging is for the weak, all my shit is rock solid, no need for logging.

  35. I use God by b1gb1rd · · Score: 1

    I keep my fingers crossed and pray to God, asking him to tell me where the error in my code is. It works 100% of the time. (You deserve this response for answering such a silly question, get back to work!)

    1. Re:I use God by T3Tech · · Score: 2

      I can see the t-shirts on ThinkGeek (which shares a corporate overlord with Slashdot) now...

      God is my debugger.

      --
      Of course I didn't RTFA... why would I do that? You really are new here aren't you? Don't let my UID fool you.
  36. As much as practically possible by Anonymous Coward · · Score: 5, Insightful

    On the other hand, broken code hurts application performance, sometimes drastically.

    I'm an SQA engineer with years of experience working with large scale enterprise systems. Generally speaking the cost of unexpected outages or data corruption outweighs the cost of hardware. In such systems the costs of deployment activity itself can be such that you'd rather pay for more hardware to support extremely verbose logging.

    Sure, boneheaded logging can cause unnecessary performance hits, the obvious example being logging in a loop when logging at entry and exit would have sufficed. But that's not what we are really talking about here. You posited that you should do as little logging as practically possible, and I believe that you are wrong.

    Log lots and log often. Just do so intelligently. Use a logging framework (log4j, log4net, log4perl etc) and set the priority appropriately. Only use ERROR for real errors (unexpected code paths or data), use WARN when a performance metric is hitting a soft limit (to warn you before you hit that hard limit), and use DEBUG to verbosely log anything else of general interest. Rarely you might also want to log in an extremely verbose manner data that wouldn't ordinarily be interesting, and this should be logged at a TRACE level. Generally speaking if this is the case then the code itself is due for a refactor. FATAL should normally be reserved for errors that prevent correct startup - generally if an application runs correctly at startup then any potential faults that you see and handle now become ERRORs because there is nearly always something better an application can do than log FATAL and exit. As the OP observed, if you have a potential fault that kills your application and you don't see and handle it then you don't have the opportunity to log FATAL anyway.

    By using a logging framework, many logging pitfalls can be avoided because the framework itself provides well tested facilities. eg, time-stamping, log rotation, file-handle management etc. In addition, using a framework allows the operator to tune the logging on a very granular level. This allows for a trade off to be made where if a performance impact is noted in a well used class then its logging can be reduced at runtime. Sure, there is still a small performance impact because the logging framework has to do a "if (logMessage.logLevel >= loggingClass.logLevel ) then {...}" comparision, but in the scale of things that impact is tiny.

    My profession is not about finding and fixing bugs. It is about understanding the software that is being delivered and deployed. It is about understanding what defects exist (or may exist) and the possible implications of those defects. It is about reducing the risk of defects through analysis. Analysis of the software's functionality, analysis of the software's performance, analysis of the processes used to produce the software itself. You will never be yelled at for releasing software with a well understood and documented defect, but the shit will hit the fan when you release major defects that are not understood.

    Logging is an _invaluable_ tool in this analysis. You'd be a fool to not use it effectively.

    1. Re:As much as practically possible by ta+bu+shi+da+yu · · Score: 1

      Someone mod this comment to above a 1 - PLEASE!

      --
      XML is like violence. If it doesn't solve the problem, use more.
    2. Re:As much as practically possible by Gazzonyx · · Score: 2

      You will never be yelled at for releasing software with a well understood and documented defect, but the shit will hit the fan when you release major defects that are not understood.

      This is the most true thing I've read in a long time. I've found that being as up front as possible about bugs and defects is always the best policy.

      --

      If I mod you up, it doesn't necessarily mean I agree with what you've said, sorry.

    3. Re:As much as practically possible by Anonymous Coward · · Score: 0

      The performance of fine grain logging can be improved by wrapping the log statement in an if() statement based on the current logging threshold.
      For example (log4j):

      if(log.isTraceEnabled()) log.trace("Complicated operation outcome=" + var.doExpensiveOperation());

    4. Re:As much as practically possible by Lonewolf666 · · Score: 2

      In my experience, once a defect is "well understood and documented", fixing it is usually easy enough. Or sometimes you can find a workaround that doesn't trigger the problematic behavior. Worst case, remove the feature that relies on the buggy code.

      If you don't have a solution, it becomes questionable to release the software at all. Unless the bug is merely a bit annoying rather than serious.
       

      --
      C - the footgun of programming languages
    5. Re:As much as practically possible by cnettel · · Score: 1

      In my experience, once a defect is "well understood and documented", fixing it is usually easy enough. Or sometimes you can find a workaround that doesn't trigger the problematic behavior. Worst case, remove the feature that relies on the buggy code.

      That's not the case if the real root of the bug is as much related to design decisions and external systems as it is a function of your own code. Sure, you might be able to circumvent the behavior or reconcile the requirements, but even after gaining a good understanding, it's possible that there is no easy way out.

    6. Re:As much as practically possible by amn108 · · Score: 1

      When you assume real people would be writing that code, you have to understand it is just too much for a simple trace call.

      Also, since fewest of projects will be switching the isTraceEnabled variable at runtime, it is completely useless to check it every time. It is either enabled or not for most cases during the entire program run.

      When doing C on Linux (since C is a preference there), use preprocessor - like

      #ifdef LOG_TRACE_ENABLED
      #define logtrace(x) log.trace(x)
      #else
      #define logtrace(x) /* do nothing */
      #endif

      It is not the worst use of preprocessor, and is a good tradeoff. In C++ i would do templates instead.

    7. Re:As much as practically possible by (0d0 · · Score: 1

      I've worked on and with a logging framework that takes advantage of compile-time optimizations such that messages below the specified log level are stripped from the executing code and messages that meet or exceed the log level are treated as non-conditional statements. This mitigates the performance impact of having to evaluate each logging line at run time, but allows for flexibility to tweak the actually level of logging by simply restarting the service. (This is a mod_perl service.) There is the disadvantage of not being able to dynamically adjust the logging level of a currently running process, but you would rarely want to do this in a production environment, anyway.

  37. Reminds me of my uni days... by Cynic.AU · · Score: 1

    Wow. This is a disturbing reminder of my University assignments, for which I forgot to remove those cout "No segfault yet!\n"; statements.

    Truly, logging is an amateur pursuit, & a grownup Software Engineer utilises formal verification and.... *GASP*.... unit testing!!

    Seriously, do the slashdot developers call a logging function somewhere with "Okay, we've started the comment routine!" in comments.pl?

    1. Re:Reminds me of my uni days... by Anonymous Coward · · Score: 1, Insightful

      Unit testing? You have to be kidding me. Next time your program fails in the wild because something happened you didn't think of, you'll go 'oh, unit testing isn't perfect after all.'

      Unit testing only prooves your program passes the unit tests. It doesn't proove it does what's required to get the job done. Even you don't know that necessarially; perhaps there's been a change to the platform, perhaps you didn't Q.A. it for d/m/y dates. Perhaps you naively assumed that GMT+13 was an illogical timezone and never checked for it. These are things logs will help diagnose; unit testing will simply cause you to think the impossible is happened.

  38. Embedded debugging by shadoelord · · Score: 2

    I work on set top boxes, and not every platform we port to has a good debugger (hell, its been years since I've seen a good debugger). Our logging system is all in house; multiple "levels" for each log statment, (noise,information,warnings,fatals,etc), with each module creating its own log id and setting its "preference level". It works well, but:

    1) Useless logs.
    Engineers not taking the time to write logs that are useful. "Got to here", "Value=1", etc. A few of us write enum-to-string functions and pass them to the logging system for cleaner output.
    2) Running at the speed of 115200.
    We've only got a serial port most times, with multiple threads trying to access it, there's got to be some synchronization, and this generally affects threads of any priority. Using a logger that caches and outputs logs at its own pace is nice.
     

    --
    this is my sig, there are many like it, but this one is mine.
    1. Re:Embedded debugging by Perf · · Score: 2, Informative

      Engineers not taking the time to write logs that are useful. "Got to here", "Value=1", etc. A few of us write enum-to-string functions and pass them to the logging system for cleaner output.

      An Engineer does something that stupid?!!!
      Who told them they were engineers? The HR Dork?

    2. Re:Embedded debugging by shadoelord · · Score: 1

      An Engineer does something that stupid?!!!
      Who told them they were engineers? The HR Dork?

      Yes, and unfortunately I don't have the power to fire them! I've worked with many a 'software engineer' that was a complete waste of space, time, and effort. A few of us started writing some more complicated tests to use in the interview process, and we've weeded out a number of applicants before they were hired. I try to stress that we always have a 90 day 'try out' period as well.

      --
      this is my sig, there are many like it, but this one is mine.
    3. Re:Embedded debugging by shadoelord · · Score: 1

      I forgot to mention a menu system that is tied into, or works with the logger and runs in its own thread. Its very helpful in a large codebase with multiple modules to have a system that you can interface to change run time parameters, get debug output, search running databases, dump profile information, etc.

      --
      this is my sig, there are many like it, but this one is mine.
  39. log it, don't forget the person who supports it by baydat · · Score: 1

    interesting topic, i have been working with a new installation of a portfolio accounting system. The developers don't talk to their support personel, who can't refer the end user (me) to how to crank up the logs to find issues and thus you find core crap laying around daily or extra process that they tell you just to kill and move on. think about your end result, software out there in the wild that is supportable, not mysterious to the end user. nice product, if it was free, unfortunetly it's ubuer expensive and i feel, is a piece of...

  40. just don't reinvent the wheel by VoidEngineer · · Score: 1

    Having spent many years as a Systems Administrator, I would argue that the most important part of logging is to make sure that it is in a format and location that other people can use. People won't use the logs if they don't know where they are. And, if you're developing for Windows, I would go further and say that the only place you should be considering logging data to is the EventVwr application. I'm not sure how the java stuff goes, but if you're developing in .NET, ditch the log4net application, and stick with the System.Diagnostic classes.

    People have spent a lot of time and effort building logging systems and installing systems to monitor those logging systems. Don't try to reinvent that particular wheel.

    1. Re:just don't reinvent the wheel by Forbman · · Score: 1

      Well, Log4Net can dump to the system events stuff, too...

    2. Re:just don't reinvent the wheel by VoidEngineer · · Score: 1

      Huh. Who knew?

      Okay, I'll amend my statement. If you're developing in .NET, and going to use log4net, just write to the EventVwr, where all the Windows System Admins of the world will be monitoring anyhow.

  41. I asked slashdot a very similar question by emmjayell · · Score: 2, Interesting

    First - let me give you my own perspective. I recommend having each subsystem log in such a fashion that you can easily grep to include or ignore that subsystem. For example for one package the following LVLx messages were the first four characters as follows:

    LVL1 - basic startup and shutdown info ( a few lines per run)
    LVL2 - Interactions with the database
    LVL3 - Interactions with the file system
    LVL4 - Detailed database interactions including each sql statement
    LVL5 - amazingly verbose debug information including memory and variable allocations

    In almost all cases, I recommend being able to set each level on or off. Your sysadmin (maybe yourself) will appreciate that ability.

    If appropriate, I recommend an 'audit' record after each completed or aborted transaction. EG - after every order or every user change or whatever is important for accountability / business activity monitoring purposes.
    This is the original question .

  42. A few things that've worked for me... by Anonymous Coward · · Score: 0

    Like other posters said, there's no one size fits all. But, I've pulled together a few tools and practices that seem to be pretty adaptable...

    Be sure to have all your logging go through one routine--don't sprinkle your code with printf's. There's more logic than you might think that you might need to add to logging: sending the log to different places, changing it for debug/release builds, and the all important consolidating reputed messages into a "Previous message repeats 29302 times."

    Once you do that, define two macros (yeah, I'm a C programmer), one that vanishes in release builds and one that doesn't.

    One trick that's worked well is to define a bitmask with one bit for each of the different things your program does. Then, add this as the first parameter to all your log calls. At runtime, set the mask to the things you're interested in, and have your log routine only print/record/send ones that match. So, if I'm trying to troubleshoot, say, something dropping messages at an end-user site, I can have them set the runtime flag to NETWORK_RECEIVE|BUFFER_MANAGEMENT, and only relevant log messages show up. This is really useful, because it lets you put lots and lots of trace in your code but keeps log files manageable. (Of course, the same log message might be relevant to a few different things. This is handled by or'ing together flags in the log call.)

    If it's code that's going to run for a long time, or pretty much if you're leaving any trace in on a shipping version, be sure to put some logic in to keep an eye on the size of the log file. Sure, everyone's got lots of disk these days, but you still feel pretty dumb when a production machine crashes after a year because the log file got to 100's of MB...

  43. Use dtrace. by Anonymous Coward · · Score: 0

    With OpenSolaris it is no longer necessary to do the actual logging yourself. Through a combination of truss (which can do system calls as well as library calls) and dtrace, the actual mechanism of logging should not be one the programmer needs to worry about.

    With dtrace, the application only needs to define points of interest in the flow of the program and export the relevant data. The collection (or logging) of data is done when it is required, so there is no performance impact when it is not in use.

    Inside the kernel, we have "fbt" (function-boundary-tracepoint) for every function - modulo optimization.

    An example of what an application can do is MySQL. See here for more details:
    http://blogs.sun.com/luojiach/entry/dtrace_integration_with_mysql_5

    This doesn't take care of every problem where logging is useful, but it does take care of a lot of them.

    1. Re:Use dtrace. by Zan+Lynx · · Score: 1

      I was going to recommend this also. Dynamic code patching is a very powerful tool and can remove the need for thousands of explicit "if(debug) log;" calls.

      Virtual machines like the JVM can do this too.

  44. Re:Attention Slash-Dot and Internet in General: by spazdor · · Score: 1

    Hey you.

    Let's have sex.

    --
    DRM: Terminator crops for your mind!
  45. Anti-pattern by istartedi · · Score: 1

    I've seen at least one example where it looked like the programmer added logging when trying to debug a problem. Then, apparently on the assumption that it was "useful once, so it might be useful again", the logging was left in. There was a compile-time AND a runtime switch to toggle this logging. It tended to be ON all the time, cluttering the logs with needless information, and making the code look ugly. In theory, compile-time switches can eliminate the performance issue; but some of the logs depended on values that had to be computed before-hand. The result? That code had to be left in, even if the log wasn't there. It would sometimes generate a compiler warning when compiling without logging, since those variables were initialized but otherwise un-used. In some cases, macros were used to dodge around this.

    The rememdy to this anti-pattern? When debugging a specific issue, I prefixed the log with the word DIAGNOSTIC in all caps, just like that. Thus, I knew that such logs should be removed as soon as the specific bug was fixed. A grep for DIAGNOSTIC would locate all these messages. The remaining logs were gradually removed, and things that were supposed to be logged under normal operation were written into the program specifications. These could be toggled at runtime via the verbose options, an option that needs to be distinguished from logs generated specificly for programmers working on specific issues.

    Code to debug a specific issue should never, I repeat, NEVER find its way into a release. Many will argue that it's a waste to throw away the code that instruments the bug; but I've found this worry to be unfounded (I'm reminded of another internet rant about "how to properly delete code", and I know I've been guilty of leaving old code around, so I'm not claiming purity on any of these issues).

    If a problem is likely to occur often in code that does not have bugs (e.g., a client that can't connect to a server because the server is down) then of course that should be logged, otherwise you aren't going to know why the client has no data. That just cycles back to what I said before, namely that you should only log what's specified in the requirements for logging.

    --
    For all intensive purposes, "whom" is no longer a word. That begs the question, "who cares"?
  46. Re:Forget logging: use an IDE, exceptions and asse by carlzum · · Score: 1

    I would argue that distributed systems make logging more important. An IDE is great when you can reproduce the error in development, but it's not always possible. A log can answer questions like "which node is exhibiting the problem?", "what actions did the user perform?", "what did the message I sent look like?". The ability to enable verbose logging is a lifesaver when troubleshooting complex, distributed software bugs.

  47. I happily use Perl by sigzero · · Score: 0

    So I either use Log::Dispatch or Log4perl (a Perl port of Log4j). Both are great.

  48. log4j, syslog by rickla · · Score: 1

    Use log4j, and use the levels you can set. It depends on what you need, but I use info for audit trail kind of stuff. And for performance put conditionals around logging for debug and other cpu hits. You can write to syslog using log4j too, for a nice central backup. I don't know where all this chatter about vm's and disk being a bottleneck come from, I have never seen that, it's just sequential writing and I do gigs of it, no issue. But if it is, redirect to a server made to store it.

  49. Log::Log4perl rocks by talexb · · Score: 1

    The CPAN module Log::Log4perl is a great tool for logging -- it means you can stick in plenty of debug statements, and dial them up for debugging, then dial them down in Production.

    This module uses several message levels; in descending order of importance they are FATAL, ERROR, WARN, INFO, DEBUG and TRACE. It's possible to log messages to files, a screen, or even to an E-Mail message.

    The real strength of this method is that you don't need a 'debug' version of the code -- all configuration is done externally, which means you can turn logging on for a Production problem, run your test, and turn it off again, and look at the log files off-line.

  50. If on Windows ... by alienfluid · · Score: 1
  51. use keys not levels. by Anonymous Coward · · Score: 0

    I use a custom logger, that doesn't log via levels, but rather by text keys that are loaded from a config file at runtime. Logging code looks like: log( "dbg", "blah blah" );
    log( "err", "some problem" );
    log( "file load", "file not found" );
    etc
    This way I can log as often as I like while keeping log entries only to the information I'm interested in simply by making changes to the log config file.

  52. Log the meaningful, email the important by Jane+Q.+Public · · Score: 1

    (1) Log lots of information when you are debugging. Then, when you are done debugging that particular problem, TAKE THAT LOGGING CODE OUT.
    If you find it tedious to find your debug log lines while debugging, it is a sign that you are logging too much everyday stuff! In the meantime, surround your debug log lines with multiple lines of "===================" or the like. Then, of course, take those out too.

    (2) On an everyday basis, log only UNEXPECTED conditions. After all: if it was expected, then your code handled it, right? That means you have to plan for unexpected things to occur. You should be doing that anyway.

    (3) If it is a very important issue, you should have an emailer set up, not just a logger. That way, you will be notified of urgent things come up.

    (4) You should be doing tests on your code, not just logging. Logs are for when problems occur. Prevent them from occurring in the first place by writing tests. If you are not familiar with that practice, look up "test driven development" on the internet. You will find more than you want to know.

    1. Re:Log the meaningful, email the important by Anonymous Coward · · Score: 0

      Don't just email the important as email may be flakey....unless you don't have a choice. Having this option is incredibly useful/desireable though for most people If you're in a shop that has a decent monitoring system, build in the ability to send SNMP traps for "the important" that are purposfully routed to a monitoring system specifically built to monitor applications.

      Scanning logs to find errors in applications can be extremely messy since all the output formats of all the apps is always different. Having the application (at a minimun) send SNMP traps enables immediate response to any monitoring application that can listen for these traps/events. Then there's no need to log the event...as long as the SNMP trap has all the important info in it (they rarely do in the real world). Logging to syslog or Windows event log are good if you're in the kiond of shop that uses them widely...

      Other than that, I'd say minimally try to make sure all your logs are both machine readible/parsable AND human readable. I like to use:
      timestamp~~error_or_severity~~message
      and then have monitoring just scan for the error_or_severity.

      If I want to include more, I add the line/module/location that logged the line.

      If you wanted to go totally nutz I'd say 'have a company wide logging format' and try to get people to stick to it. Go the whole enchilada: How to name the logs, how to rotate them, etc...it would be easy to monitor and anyone could jump from 1 app to another and maintain "logging sanity"...support would love it...same with SNMP: mandate all in-house apps send SNMP traps for errors with certain information "always" in them...but that's never gonna happen.

  53. ONLY IF... by Jane+Q.+Public · · Score: 2, Funny

    ... you are of the opposite sex, have no significant communicable diseases, and pay your own way.

    1. Re:ONLY IF... by CTalkobt · · Score: 2

      Just as an explanation for those who haven't clicked on Parent enough times to read the original post...the original thread stated "I am horny" ... hence the above comment.

      He was moderated off-topic, which seems fair however this being slash-dot I thought redundant might be a better moderation.

      *ducks*

      --
      There's a gorilla from Manilla whose a fella that stinks of vanilla and has salmonella.
  54. Bunion Software's Blue Ox Lumberjack Logging by c0d3r · · Score: 0, Offtopic

    Checkout Bunion's Blue Ox Lumberjack Logging Solutions for industrial strength Logging functionality.

    http://en.wikipedia.org/wiki/Paul_Bunyan_(lumberjack)

  55. operational errors by Spazmania · · Score: 2, Informative

    1. Distinguish between serious -operational- issues and other issues. The sysadmin doesn't need to know that you had a pointer problem; there's nothing he can do about it. He does, however, want to know that a message was received and the appropriate action taken. Or that a connection was attempted but failed.

    2. Be grep freindly. The first log entry related to a particular activity should have an ID of some sort in the log message which is then included in every additional log entry associated with that activity.

    --
    Moderating "-1, Disagree" is simple censorship. Have the guts to post your opinion.
    1. Re:operational errors by ToasterMonkey · · Score: 1

      I'll second those.

      Please, don't ever just print raw exception or stack trace in the same log files your operations people are expected to read/grep for serious issues. Just think for a second who you are writing to when you decide which direction to print a given message.

      It's 11PM, and reports of customer connectivity issues are heard.
      The late shift sees repeated IOException, blah, blah, blah, in the log file of our messaging application, do they:

      A. Ignore it, assuming the client's end is messed up and dropping connections.
      B. Twiddle with the load balancer, assuming there aren't problems in the back end affecting multiple nodes.
      C. Start waking up developers because for all I know it could be dropping database connections.

  56. Tagged based logging by NightStorm · · Score: 1

    I used a system where log messages were assigned arbitrary tags which were themselves strings. The communications subsystem got the "COM" tag, the memory manager got the "MM" tag, etc. Log levels were just tags ("ERROR", "FATAL", etc). The system supported 32 output channels, each channel was associated with some output stream (file, pipe, etc) and each had a Boolean expression which filtered what went to the channel ("FATAL" or "ERROR" or "ALERT" and "MM" or "COM"). A channel could also be assigned a "lifetime" so that output to the channel only lasted for some period of time. Each channel could be setup as the daemon was running so it was easy to start debug messages going to a particular console, memory manager messages to a file, alerts to a pager app., etc. It was great when remote accessing a running system and diagnosing a client's issue. When you are all done you just shutdown the channel or let the channel expire. End of output and no more wasted I/O.

  57. Verbosity levels by Almahtar · · Score: 1

    Good general advice in logging is to support different verbosity levels. Provide the ability to change the log verbosity as a command line switch or as a signal the process can receive.

    Then if your log is too cluttered, reduce verbosity and try again. If it isn't telling you what you need to know, increase verbosity.

  58. Start recording before you need it. by evilad · · Score: 1

    One of the coolest things I ever saw was a tape recorder that starts recording 30 seconds before you press "record". Simple concept -- just continuously record to a 30-second buffer.

    So if it's truly disk/network that's at a premium, why not start logging just before you need it? Log at level "EVERYTHING" up into a circular buffer, and only dump to disk or network when there's an error?

    1. Re:Start recording before you need it. by ciggieposeur · · Score: 1

      That is a fantastic idea! (goes to add feature to my logger classes...)

  59. What a real logging library should have by Anonymous Coward · · Score: 0

    Support for syslog, syslogng, windows event log, log to file, console, RDBMS.

    Messages should be routable to multiple sources based on defaults or logging call.

    It should have standardized message severity levels and parameterization for location, session context..etc.

    It should support threshold supression for classes of messages to prevent log spamming.

    It should provide a transactional approach where multiple writers are posting to the same log file at once the actual output should be coherent by transaction.

    It should provide a queuing option to background delay and failure prone posting to disk, network, RDBMS...etc and provide backup logging paths.. It should not introduce delay in async environments.

    It should provide buffering options to enable or disable flush to disk and buffered writes for higher performance logging.

    It should be thread safe and optionally never dynamically allocate memory.

    It should support message id tagging for message localization.

    It should provide bindings to post SNMP traps.

    Unfortunately TFA falls far short of what I think is a reasonable base-line feature set for a logging library. I wonder if there are any public domain/LGPL libraries out there that come close?

  60. Automated Testing by Mia'cova · · Score: 1

    To me, it's important to cover both the internal and external cases in any scheme.

    For the internal, it's important that your organization is capable of finding owners for failures/bugs quickly. So when a developer hits an error they don't understand or your BVTs break, an owner can be identified. Nine times out of ten, they can get you a solution in an hour or two once they've seen the error. In addition, an automation framework should work closely with your logging to bubble up the relevant errors. That can save a LOT of time.

    For the external case, it's important that the errors are professional. A casual user won't dig into logs. But technical users (admins, etc) will rely on your output. The logs should be search-engine friendly. I recommend unique error codes for any log output. Localization is not always appropriate as it can cause problems for non-english searches. If support needs to involve developers, it also helps if these logs are in the developer's native language.

  61. Be careful what you log... by gillbates · · Score: 3, Interesting

    It might come back to haunt you later.

    • If it could be useful for invading someone's privacy, law enforcement will expect to have access to it. Consider, for example, that the RIAA lawsuits were enabled by the fact that ISPs logged (and kept the logs) of the IP addresses and to whom they were assigned.
    • Consider how an attacker could use the logs as a means of figuring out how your software functions, and how to defeat it. "Buffer overflow on line __LINE__" - while well-intentioned - would be a bad message to log. If you must log defects, sanitize the output so that it doesn't reveal more information than is necessary for the reader to know.
    • In general, don't log normal operation beyond the startup and initialization messages. Users have this uncanny habit of looking to the logs for ways to blame the software for their own ineptitude. A verbose tirade of log messages will often provide them with ample ammunition for blame deflection and result in unnecessary service calls and potential misunderstanding. Especially if the log messages are cryptic and could be misconstrued to indicate that there is a bug with your software.
    • With respect to the above point, make sure that any errors which could be caused by misconfiguration or user error clearly indicate that the problem lies with the user or configuration.
    • When you do have to log a bug, include only the technical information necessary for the person doing the debugging. The point is to make it sufficiently technical that you have a starting point for debugging without giving the end user something with which they can hang you. Reporting a seg fault won't do you any good if you don't have the means to determine where and why it happened.
    --
    The society for a thought-free internet welcomes you.
  62. One Form of logging is not enough by dtm789 · · Score: 1

    I work on my company's billing system and we use both database and file logs. We process millions of transactions a month so it's important to not only have a detailed audit trail of every transaction but also be able to keep track of any errors and be able to recover transactions from logs in the case of something unexpected happening.

    For file logs we use Log4Net and I recommend it. It allows us to specify log statements as Warning, Debug, Error, or Fatal. We have it set up so that all types are logged in the file while any Error or Fatal log statement is emailed to the engineers so we can immediately look into the situation. I don't believe in having too many log statements as it does nothing but make finding useful information difficult. As a general rule we log as much information as possible about each transaction as it comes in and out of each part of our system as well any major changes to that data that come from a part of processing it.

    Database logs are extremely useful because they can be queried unlike file logs. This makes finding the information you need much easier. In our case however the db logs only contain information about the transaction and not the processing of it. Therefore both the db and file logs are necessary.

    Having two types of logs also acts as a fail safe. If our db goes down we still have file logs. If there is no trace of something in the file logs we can check the db.

    Now with this level of logging comes other issues such as archiving logs and dealing with db log tables that are too big but that's a topic for a whole other discussion.

    1. Re:One Form of logging is not enough by Forbman · · Score: 1

      files can be "queried" with the correct tools, too... awk, head, tail, grep, etc. (yes, there are even Win32 versions), or even Linq these days.

  63. log4j FTW by cranesan · · Score: 1

    I frequently use log4j or it's non-java derivatives in different languages. No need to reinvent the wheel when log4j properly handles logging with probably the most flexibility and performance anyone could ask for.

  64. None Inline, Use AOP by xero314 · · Score: 4, Interesting
    I just wanted to second the "as little as...possible" sentiment and take it one step further. DO NOT add any logging to your application code. If it is not essential to the logic it should not be in the code.

    That being said, you can add what ever logging you want as long as you do it outside the application code, such as through AOP (if you don't know what that is then google is your new best friend).

    Logging through external means has a number of benefits. First you application code is relieved from unnecessary clutter. Second the logging can be added or removed as fit for the environment with no need for any runtime checking, which is perfect for turning of all logging in production environments. And lastly it enforces good coding practices as it makes sure people break up code in a way that makes external logging possible, which is how code should be written anyway.

    The majority of all logging, dare I say all useful logging, is easy to summarize.
    • Starting Process... with the following conditions...
    • Starting Process... with the following conditions...

    This is perfect for function/method interception since it comes down to something more specific.

    • Executing Function... with the following arguments... (with one of those arguments being the state of the object the function is attached to in Object Oriented Programming)
    • Function... returned the following...

    So simply add the first logging to an interceptor that operates before the functions you want to log, and the second after the function.

    If you find yourself needing detail inside the function then you need to break the function up into sub routines, so you can use this generic logging on the sub routines instead, or as well as.

    1. Re:None Inline, Use AOP by Jonboy+X · · Score: 1

      If you find yourself needing detail inside the function then you need to break the function up into sub routines, so you can use this generic logging on the sub routines instead, or as well as.

      So, just to be clear: you're advocating breaking up your software into otherwise-unnecessarily fine-grained methods, because it's less intrusive than just adding logging statements? Sounds more like a recipe for spaghetti code.

      Somebody drank the AOP Kool-Aid...

      --

      "In a 32-bit world, you're a 2-bit user. You've got your own newsgroup, alt.total.loser." -Weird Al
    2. Re:None Inline, Use AOP by xero314 · · Score: 1

      So, just to be clear: you're advocating breaking up your software into otherwise-unnecessarily fine-grained methods, because it's less intrusive than just adding logging statements? Sounds more like a recipe for spaghetti code.

      Actually what I am suggesting is that breaking up your code in this fashion actually leads to cleaner, more readily reusable code, that just happens to be perfect for allowing the necessary level of logging. I suggest that the same rule applies to commenting. If you find the need to for an inline comment than you code is more complicated than it needs to be and should be broken up into smaller components.

      Well segmented code need not be spaghetti code. Spaghetti code is a reference for difficult to follow control structures and has nothing to do with segmentation.

      I have one simple rule when it comes to code: If it is not required for honoring the contract of the procedure or directly influencing the required logic, do not put it in the code. This goes for configurations as metadata (like java's asinine annotations), premature optimizations (result caching), insignificant commenting (any inline commenting), as well as logging.

  65. Debug Levels? by Anonymous Coward · · Score: 0

    I've used lots of open source software where when you increase the debug logging level the verbosity and/or additional information is given as the level increases.

    I guess if you needed to isolate certain output then you could output the debug level of the given log entry, then pipe to grep to search?

  66. Good logging is worth the time by Anonymous Coward · · Score: 0

    I develop for a large ERP application where we allow the user to control the logging level. In normal operation, we do minimal logging to save disk space. When a problem arises, the user can choose from several levels of detail in logging. Logging is an expected part of any code we write and it enables both our support department and development team to debug issues quickly.

    At the most detailed level for a server process, we show SQL statements and number of rows affected with every iteration in any cursors (which we avoid whenever possible anyway) and output current values of relevant variables. For server processes, logging level can be set for the entire datasource, and overridden per execution of the server process.

    Client side logging is used less frequently but can be set via a registry key by our support staff when trying to debug problems. Client side logging will output XML of any datatables in memory at relevant points, relevant variable values, and call stacks.

    Once you have a framework for logging created and get in the habit of using it, it requires very little extra work and is well worth the effort when things go awry.For the average server process or screen we do, adding logging logic probably adds only a couple of hours to the project, but over the lifetime of the product, it will save us dozens of hours or more in wasted debugging time.

    The logging is useful testing phase as well.

    The important thing is to create a standardized logging system for your product that is easy for developers to use so they don't have to spend much time thinking about it and it doesn't obfuscate the logic in the code. The company I work for requires developers to provide support for some of the software they write, and this seems to provide the necessary motivation for us to use our logging mechanisms to their full potential.

  67. Logging for debugging by Tofflos · · Score: 1

    If you're logging for debugging purposes I suggest setting your application up so that you can change verbosity on the fly. Combine that with a good naming convention within log4j and you could increase the detail in an area of interest while filtering out the rest.

    Some parts of your application might not need constant logging. Put the logging code in there anyway and use filtering to keep it from ending up in the log. If you ever run into problems just remove the filter.

    If you are communicating with other systems figure out how to tap into that conversation. They might work fine now but sooner or later they'll break.

    In the root of your application put a document labeled "HOW TO READ THE LOGS FOR TROUBLESHOOTING". Make sure the filename stands out from all the other files in there. Explain where and how to do the things mentioned above. It might be someone else than you that has to do the troubleshooting. Or you simply forgot how to.

  68. Logging is for pussies by Anonymous Coward · · Score: 0

    Real men sling their code from the hip then flex in a mirror after they're done. And if I find a bug, I pry it out with tweezers I've sterilized in a camp fire.

  69. Log event-related info and provide for logging dec by RAMMS+EIN · · Score: 1

    This is something I have recently been thinking about quite a lot. I am used to being able to easily debug software and watching what it is doing, and I had always considered logging as something you do because...well, I actually didn't really know why.

    Now that I have done commercial development on larger project for a while, I have developed a somewhat better understanding of what you want logging for. I am still figuring it all out, but these are my current rules:

      - Log event-related information. When your program receives input that causes it to start doing something, log that. If there is something strange or wrong about the input, log that. Once you stop processing (because you're done, or because you can go no further), log that. This way, you can see what your program has been doing. You can see that it received input (very important), you can tell if your program thought the input was correct or not, and you can see that it finished processing the input, and whether processing was successful or not.

      - Provide for the option to log information about decisions your program makes. This is particularly useful for finding out why your program did not take action on something. This shouldn't always be enabled (unless, perhaps, for high-level decisions), because it would make logging overly verbose, but you should be able to enable it (preferably, focused on a specific part of the program) if you think the program is not behaving as it should.

      - Last but not least, log every error condition. This should be obvious, but it is very important, so it's worth stating explicitly. You want to know if something went wrong, you want to know what went wrong, and you want to know where in your code it went wrong. Backtraces are very helpful here.

    A large part of this is actually Cover Your Ass. If you log all events, and you didn't log anything, you can tell the Exalted User that the program didn't do anything, because it didn't receive anything from said user. If you validate input and log errors, even non-serious ones, you can tell the user "try with valid input", while you think about the problem and look through the code, seeing if anything is wrong on your end. If you log all errors, you can say "well, it says here there was a problem with the database connection, have a look at that", or "hmm, that value wasn't filled in, but should have been. are you sure you filled out all required fields?"

    The above might sound disrespectful, but it really isn't. Very often, the users actually are doing something wrong, and they would be happy to know how they should do things to make it work, if only you could tell them. Logging everything you need to establish exactly where the problem is is invaluable for this. Your logging should tell you if the error lies inside your program or outside it. This makes your life easier, but your users', as well.

    Finally, I will add that I very much agree with AaronLawrence's comment.

    --
    Please correct me if I got my facts wrong.
  70. Completely depends on your needs, as always by caywen · · Score: 1

    Logging should be fit to your needs - anyone who tells you that your logging is wrong is probably wrong. For example, I took a lot of flak in my company for having *extremely* verbose logs. Our support, who like to look at logs, blamed me for making the logs way too huge. I then got the linux server guys lecturing me about the proper way to log - I apparently should only log critical errors, and I should have several different log levels, right? WRONG. The product in question is a plugin, and relies on very flaky API's. It is subject to interference from other plugins and bugs in the host application itself. Having several log levels only lead to too much back-and-forth between support and customer (ok, now try THIS log level. ok, now try THIS log level. ok try...). So, for my needs, where anything can go wrong at any time, super verbose, scope-indented logs were absolutely necessary. For other needs, only critical errors need be logged. And every need in between has justification as well. There is absolutely no rule, and as long as your logging meets your needs, no one has any business telling you how to do what they don't understand.

  71. First determine who the logging is for by caywen · · Score: 1

    First question is: who is the logging for? Sometimes the logging is for the customer, sometimes it is for support, often it's for the developers, and often it's for some combination of these. If it's for the customers, the readability, volume, and performance are all considerations. But if it's for the devs, requirements can vary greatly. Some people here say, "what about CPU" and others say "what about disk space?" If I'm logging for an app on a 5 petabyte system that only runs a relatively small chunk of code once per day, I'll go ahead and log every line if that helps in some way. If I'm logging for an embedded app with about 32k of free space, I'm going to log very differently.

  72. Re: Software Logging Schemes? by aakhan · · Score: 1

    In my previous office, we wrote and deployed real-time, mission-critical applications the likes of core-banking middlewares and ATM controllers, to name a few. Once deployed and set live on live banking servers, generated logs from those applications were the only means of figuring out and debugging, when something went wrong, what went wrong, how, and when. In fact, it was standard procedure to follow for the banks' IT staff when reporting any problems they encountered to open a support ticket, and send in logs.

    There is no easy and sure way to determine what in the application needs to be logged. Like many things with software development, it comes through experience. As the products matured over the years, we, back in the previous office, realised what the bare minimum set was in terms of what should be logged. And that naturally helped in developing a simple pattern for what needed to be logged when a diverse set of people added extensions to the applications in terms of modules, etc.

  73. Logging... by Anonymous Coward · · Score: 0

    I restrict mine to stop global warming.

  74. Eh.... by ZipprHead · · Score: 1

    I've been using log4net and log4j for years. Generally:

    I log info at the start of every function logging all parameters passed it.
    I log info at the end of every function logging return values if applicable
    I log errors and exceptions at a high level

    I NEVER trap the exception entirely, every tier (including the end user) needs to be aware that an error occurred, be it their fault or the programmers.

    just my 2 cents.

  75. Here is an excerpt from a guide that I wrote... by Ixitar · · Score: 1

    Exceptions

    All exceptions should be handled, it is never acceptable to simply print the exception message and stack trace. Exceptions should be dealt and corrective or informative action taken to highlight the issue.

    For debugging purposes the stack trace should be logged at the final destination of the exception or at whenever the exception is modified.

    Logging

    If a class has unconditional logging, then it should be updated to use log4j. A case of unconditional logging is where there are System.out.println() in the code with no conditions surrounding the call.

    Logging should always be performed at method exit and entry as follows:

    - Log entry
    - Log arguments
    - Log exit
    - Log return values

    The occurrence of exceptions and the stack trace should also be logged as info level items.

    Logging calls should be wrapped in enablement checks so that arguments do not get unnecessarily evaluated for example:

        If (getLogger().isDebugEnabled() {
            getLogger().debug("Entering - method - argList");
            getLogger().debug("Arg1 :"+ theArgValue1);
            getLogger().debug("Arg2 :"+ theArgValue2);
        }

    Assertions

    Assertions should be used in the code to verify that the expected results have occurred. Assertions should be used as liberally as possible.

    Standard Assertions should be performed at method entry and exit; these methods should validate the pre and post-conditions for the method. All arguments should be checked for validity as should the return values. Similarly the state of the object and broader system should be checked as appropriate on both method entry and exit, for example if a file is open.

    Note

    Please note that this is in using log4j where the logging can be turned off an on at different levels of the code during runtime.

  76. New programmers vs experienced programmers by SloWave · · Score: 1

    Maybe it's just me, but one thing I've noticed is that new programmers tend to use very elaborate and extensive logging code with logging calls that take a zillion parameters like log level, error type, error code, short message, long message, so-one and so-on. The experienced programmers seem to use something like fprintf(stderr," log message... . That combined with maybe a few compile time macros. Of course the their code may just need less logging anyway.

  77. Desk check, Code review, debug, Log, Spy by peterofoz · · Score: 1

    Firstly I agree with a previous poster that using a debugger is better than logging for most development work. Single step through code to make sure its doing what you expect.

    At the risk of exposing my age, I'm also a big proponent of desk checking code and algorithms. (Yes, we had limited mainframe CPU minutes to run our card deck and paper tape applications in lab assignments in college). Use a design review and code walk through process to explain yourself. It's amazing what you'll find as your colleagues challenge you on design decisions and you have to defend it.

    • I use logging in my apps in two ways:
    • Module entry/exit values
    • Around calls to external systems (database, web services) to capture timing info and results

    With good code structure, this should provide a reasonable level of logging so you can determine where something might have gone wrong or gotten stuck in production or test out of reach of debuggers. Sometimes, bugs are timing related or only occur during stress testing so logging is the way to go as a modern replacement for the hex crash dump. You should be able to enable/disable logging with an option for verbosity. You can log to a text file or to something like the windows event log.

    Spy utilities are also useful logging and debugging tools. There are tools built into ODBC for logging transactions client side or tools on the database servers. For SOA there are SOAP spies. I use Mindreef. Another option is Altova.

    Moving forward, we're building workflow systems using FileNet Visual Workflow (or P8) or BPEL (Oracle, Active Endpoints, etc). In these cases we fork asynch child messaging workflows that log events to a database without holding up the main workflow process. These are inserted at key points in the main workflow and usually double as process monitoring points used in various process management reports.

    Finally, debugging, logging and spy tools are no substitute for poor application and code design. Include module block comments and descriptive variable names at a minimum, and a sprinkling of line comments for the tricky parts.

    You can put lipstick on a pig - you will still have a pig.

  78. Comment removed by account_deleted · · Score: 1

    Comment removed based on user account deletion

  79. Logging for an OS by clusteroid81 · · Score: 1

    As an OS developer, the type of logging done is very dependent on where in the OS the logging is going to occur. In many cases, the rate of logging events can be extremely high - exceeding millions of events per second (think of logging operations on OS semaphores on very large SMP systems). Other tracing events (such as in the IO subsystem) events are occurring at more reasonable rates (1,000s of events per second).

    For logging high frequency events in the OS, logging to disk is typically not a feasible option. For these events, we log data into ring buffers in memory. The records in memory are typically fixed length. An atomic increment operation is is required to obtain an index in which to write the log entry. A small header typically consists of a time stamp, a CPU id, and a record type. The rest of the data could be either formatted text or binary data. In the case of binary data - some additional display code is required to format or analyze the data. Under normal conditions, this tracing is typically not turned on - the performance impact is just too high. The tracing is usually turned on during development testing and possibly beta testing or when attempting to reproduce a bug where the trace data might help. Tracing is turned on typically by allocating some memory that contains a pointer to the trace data, and this memory has a bit that indicates if tracing is turned on. If the structure is not allocated or the tracing bit is off, then no tracing occurs. Thus the test to determine if tracing is on is at most two trivial read operations - make sure this data is in a cache lines that contains no data that is written. Most of these reads will then be satisfied from cache and thus be very fast so the performance impact with tracing turned off is very minimal.

    For the ring buffer itself - the size dictates how much data is retained. In most cases, only a few megabytes of trace data is allocated. However, in some of the high frequency cases where a specific problem is not noticed within the OS for some time - 20-30GB of memory has been used for trace data. Note that this is physically allocated memory and not something that is pageable so there the system needs to have this much available free memory. In these cases, tools to search the trace data are also necessary.

    Being an OS debug tool - if tracing is turned on and the OS crashes - the trace data is logged in the crash dump and accessible (our tools can format this data from the dump file). Should the bug being tracked not result in an OS crash, then when software notices the bug, the bit indicating that tracing is turned on can be cleared thus stopping the trace data as soon as the problem was noticed and hopefully preserving useful data in the ring buffer.

    With numerous trace facilities and then many options within each facility - only tracing that is needed to debug an issue needs be turned on. As this code is shipped with the OS, should a customer be having an issue and the issue can not be reproduced in house, it is often a great benefit to have the customer turn on this tracing.

    With a trace routine that is basically a printf to memory - should some additional trace code be required, it is easy to go add addition trace code to the OS and rebuild the necessary components and reboot the system - for my specific OS, this can be accomplished in just a few minutes.

    The above tracing mechanisms have been very valuable to debugging. This type of tracing can also provide quite a bit of insight into how the OS operates (especially for large SMP environments) and provide performance data on the system. In the semaphore example, if the semaphore address is recorded along with information on when the semaphore is acquired and released, then reporting tools can be produced that show the percentage of time a particular semaphore is held. If CPUs are spinning on a semaphore and that event is also recorded - then the ability to report the percentage of time CPUs are waiting on semaphores is also pos

  80. More is better by canuck57 · · Score: 1

    1 line of logging per 200 lines of code is way too much. 2 in 5 lines is absolutely insane. I've seen way too many systems where the logging made it totally unusable. People just don't realize the costs of logging everything.

    There's absolutely no way to document anything this verbose. Without documentation, the logging is useless.

    Huh?

    Depends entirely on logic, not generalized statements. If you're into a potentially problematic section of code, 5-10 log statements per 100 lines may not be enough. If you are in a section of code that is verbose code but not much to go wrong, the logging statements should be far fewer. Good logs is essential to debugging and problem determination and more is generally better than not enough so generalization is not going to fit here.

    And in the code, just because there is a log statement does not mean it generates logs, add filters to the approriate level,

    if (confLogLevel <= LOG_ALERT) logIt(...);

    isn't that hard to implement is it?

  81. Make it configurable by Anonymous Coward · · Score: 0

    You can refer to my project at http://realeyes.sourceforge.net/ for details, but essentially, I make the amount of logging configurable. Each message is defined with a level, such as CRIT, ERR, WARN, INFO, etc. Then in the config file, the user can define whether or not to receive INFO and/or WARN messages. That way, when the app is first run, everything can be logged to be sure that it is working correctly. Later, the logging can be reduced by ignoring INFO and even WARN messages.

    Later . . . Jim

  82. lynching or not? by Anonymous Coward · · Score: 0

    I often wonder what my peers would say about the logging I implemented on the system I've maintained and ported. Some psuedocode below:

    bool Function1 (string s, int i, ref ERRORLIST e)
    {
        try
        {
            if (!Function2(s, i, e))
            {
                throw new Exception("Call to Function2() failed.");
            }
            return true;
        }
        catch (Exception f)
        {
            STATICDB.Log("who", f.Message, "Function1", System.DateTime.Now);
            if (e != null)
            {
                e.Add("who", f.Message, "Function1", System.DateTime.Now);
            }
            return false;
        }
    }

    The point here is that the exceptions are logged to a database, but also to a list that travels back up to the caller. If this error happens pretty deep, the list presents a chain of events that caused it. It's an optional argument if you don't want the user to see anything but a failure, but we've found it helps them help us figure out what the hell caused whatever happened and, in the case of business rules, what we need to fix or accomodate.

  83. Log by level by davidwr · · Score: 1

    The specific scheme doesn't matter, but you want to log when certain conditions are true.

    Something lime this is at the heart of many logging schemes:

    Have a function LOG(flag,text).

    LOG compares flag with a global/static variable CURRENT_FLAGS which is set during initialization or perhaps in response to special log-level-setting commands. If flag is in the currently-active flags, log the text, otherwise, discard it.

    Typically, all logging that is debug-only will be surrounded by compile-time "#if DEBUG/#endif" or equivalent, so they aren't even compiled into non-debug builds.

    --
    Knowledge is how to play a game, intelligence is how to win, wisdom is knowing what game to play.
  84. It Depends by natoochtoniket · · Score: 1

    Some comments, above, seem to say that every app should produce Mitchner-novels-per-second of debug and log. Others seem to go to the opposite extreme. Both are right, and both are wrong.

    There are some applications, and some portions of most apps, that have extreme performance issues, and for which excessive logging is likely to produce a problem in production. And, there are some applications, and some portions of most applications, for which very detailed logging is needed to meet specific diagnostic or auditing requirements. And, there is a lot of gray area in between those extremes.

    We use a variable approach. We make it very easy to code a "debug" or "log" output -- It's just a one line call. And, those routines have the first parameter for the "level". A separate call is available to set the desired level for the process. Level 0 message are reserved for major events and severe errors. Level 1 is for intermediate level events and warnings. Level 2 for minor events and warnings. Debug output starts at level 3 or 4. Inner-loop debug output is at level 5 or 6. At level 5 or 6 you get the proverbial Mitchner-novel-per-second. We generally use level 4 on for dev testing, and level 0 or 1 on production systems.

    Efficiency is a key, both in execution and in coding. The code is small and easy to write -- only one line of code per message. And, the logging routine itself is very efficient in the case where it decides not to print the message because the level is higher than the current per-process level. A message that is not printed consumes just a few machine instructions.

    In new code, we typically have a logging call per three or four statements. In difficult algorithms, maybe more. In straight-forward byte-moving code, maybe less. As code becomes more mature, and we become more confident that it is right, we might comment-out some or even most of those calls. When a piece of code is under investigation, and the developer doesn't quite see the problem yet, I often advise him/her to add more debug logging to that area.

  85. (trace ...) by chkn0 · · Score: 1

    There's no need to clutter up source code with logging statements for debugging. Just trace appropriate functions as needed.

  86. Talk to IBM? by TheLoneGundam · · Score: 1

    A long time ago (in 'Net time anyway), there was a Slashdot article regarding what IBM could do to help the open-source world, now that they are heavily into Linux. Here's one place they may be able to help, through experience if not actual code. On z/OS operating systems, there are two (or three) logging architectures: there are instructions and supervisor calls for low-level tracing (Generalized Trace Facility) which is a high-overhead debugging tool. There's the System Management Facility, which is a software standard for logging things like utilization data, events such as file open/close (with I/O or media error statistics), etcetera. SMF would be the closest to 'normal' logging. There's then a 'System Logger' facility which handles log files of all sorts, cycling them, etc. This kind of standardized logging architecture might be a good thing for Linux, also.

  87. A philosophy of logging by Zarf · · Score: 1

    You should be able to dial logging up and down. Use log levels to this advantage. If you are in C++ use compiler pragmas to remove trace logging statements. When doing trace logging you want very verbose output but that's only helpful to you. Think about the application as it ages in the hands of an administrator.

    If your application pumps the log full of messages all the time eventually your administrators will start ignoring the logs. They'll be information saturated and unable to distinguish the errors from normal operations. This is bad. In production only log problems and provide plenty all the context you can when you do log.

    Your administrators are users too and they only have so much time in a day. You want to only complain to them when something very bad has happened. You want them to sit up and pay attention when your application burps out a log.

    Obviously, I've presented the most extreme case... there are points on this continuum of silent to verbose that make sense for different situations. Use your judgment and be mindful of your administrator's "information budget".

    --
    [signature]
  88. Roll your own! by Abcd1234 · · Score: 1

    It's the only way to fly! It's like linked lists or quicksort implementations... why reuse a well-tested, properly engineered implementation when you can build your own from scratch?

  89. TraceMessage() is efficient (MS Windows) by pg--az · · Score: 1

    At least in Windows XP, I have found that the TraceMessage() function seems to run nicely with the MessageFlags parameter set to zero, meaning that only the USHORT MessageNumber identifies your message, thus minimizing log-file-size. But if you're the only app logging to your ETW session then 64k message-types are enough, why would I need a 16-byte GUID in each message ?! By comparison only TraceEvent() was available in Win2K, significantly more overhead. I also briefly examined EventWrite()( VISTA ) and it seemed way more bloated than even TraceEvent(). According to MSDN TraceMessage() continues to be officially supported in Vista and server-2008, so there is still a way to do fairly lightweight tracing. It's definitely nice to have these kernel-logging-sessions just keep on ticking while your apps crash.

  90. Log the screen at time of error by chris.evans · · Score: 1

    that way you can see exactly where the user was at when it crashed.

  91. Instrumented Code is what I write by Douglas+Goodall · · Score: 1

    For the last ten fifteen years, I have been big on debug code writing html pages into the web root so that the developer can locally browse the debug data. Emitting code with links makes it easy to get around. I have used this extensively and it works very nicely.