Logger for Objective-C

Discussion in 'Mac Programming' started by sivaprakash, May 9, 2011.

  1. macrumors member

    Joined:
    Mar 10, 2011
    Location:
    Chennai, India
    #1
    Hi

    I am looking for a logger mechanism to be incorporated in my application. Can I use http://log4cocoa.sourceforge.net/? Or any other better framework available out.

    Thanks
    Siva
     
  2. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #2
    What are you wanting to do? Just output the equivalent of an NSLog() function call to a file?

    If you just want to do that, this is what I do. I use categories to add a -log method to NSObject. Then I use something along the lines of this code

    *Untested* Its C because I can never remomber the method in Objective-C to write strings to a file.

    Code:
    //Setup log file
    FILE *file; 
    file = fopen("log.log","a+");  
    
    //Write to file
    fprintf(file,"%s\n",[[self description] UTF8String]);
    
    //Close the file
    fclose(file);  
    
    
     
  3. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    There's the Apple System Log facility. Enter the command "man 3 asl" (without the quotes) in Terminal for details. It's Apple's replacement to traditional syslogd, and is one way of getting a server process to log into Console.app.

    I'm not recommending it one or another, just putting it on your radar.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I think the log4 style of logging is great. We use it (log4perl) in a very large application, and I'd say the feature I like most is being able to raise and lower the log level in your configuration file while a process is running and have the level change in running processes immediately. I don't have experience with Log4Cocoa, but log4j and log4perl are both good, so hopefully the Objective-C/Cocoa implementation is as good.

    -Lee
     
  5. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #5
    Another +1 for log4. We use it in Java and it's great.
     
  6. larkost, May 9, 2011
    Last edited: May 9, 2011

    macrumors 6502a

    Joined:
    Oct 13, 2007
    #6
    A while ago I wrote up a little article on improving on NSLog(). If you are aiming at going through syslog (strongly encouraged), then this can help you out a bit.

    After looking over log4cocoa, I really think that I was on the right track. By using the ASL system you already get logging levels (note: what gets outputted to /var/log is just the tip of the iceberg with ASL), and you can just use C calls (works just fine inside objects). Plus it works inside XCode, and since it uses pre-processor Macros if you use the debug level then lower debug levels just get compiled out (as in not in the file at all). And ASL already has the concept of log levels, and things below a threshold never make it to text log files (while remaining in the database for a while).

    Log4 might be great for people coming from other languages/platforms, but Apple has built so much in that most of its features already exist.
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    It sounds like the log level is determined at compile-time. The best part of log4 for me is being able to turn logging up when you need it without rebuilding.

    -Lee
     
  8. macrumors 6502a

    Joined:
    Oct 13, 2007
    #8
    You missed the whole part about ASL allowing you to "log" things without them appearing in /var/logs (just persisting in the databases for a little while). So you can have "debug" builds that spew everything to stderr, and then "production" builds that spew less information into the database, and still less information into /var/logs (with ASL already providing the ability to have multiple custom files with different formats and levels of information).

    In other words, you get all of that functionality already with ASL, you just have to wrap it a bit (hence my little bit of work).

    Anyone who has not read up on ASL, should read this nice article on it.
     
  9. thread starter macrumors member

    Joined:
    Mar 10, 2011
    Location:
    Chennai, India
    #9
    Hi

    I have written very simple code to write my log, but i dont have any clue where it is getting stored. I tried to trace under Var/Log but there are large number file available hence couldnt find the right one. Can you please let me know where it will be kept?

    Code:
    
    #import <Foundation/Foundation.h>
    #include <asl.h>
    #import <stdio.h>   // for printf()
    #import <syslog.h>  // for LOG_ constants
    
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        // insert code here...
        NSLog(@"Hello, World!");
    	
    	asl_log (NULL, NULL, LOG_NOTICE, "Test Message");
    }
    
    
     
  10. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #10
    The asl_log message will be logged into the system console. You can access it via Console.app in /Applications/Utilities. Search console message for the name of your target. Save the database search for convenience.

    Where the NSLog goes depends on how you run the program. If you run it inside XCode, it will be go into the debug area of XCode 4 or the debug console of XCode 3 (not to be confused with the system console mentioned previously). If you run it outside of Xcode, it will go to standard error, or to where ever standard error is directed. Note the launchd redirects standard error to the system console, so under launchd your NSLog would be the same as your asl_log.
     
  11. thread starter macrumors member

    Joined:
    Mar 10, 2011
    Location:
    Chennai, India
    #11
    Yes I can see the messages.

    Is there any way for me to write my logs into File using "asl_log"? So that I can control Log file size and categorize them.

    Thanks
    Siva
     
  12. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #12
    I've not done it. I've only used this to log messages that a sys admin might want to use for post-mortem or forensic analysis. There is asl_add_log_file which will add a file descriptor to an aslclient. You get an aslclient from asl_open.

    You can also configure syslogd to store log messages from your program into its own log file. Man syslogd, syslog.conf and asl.conf for details.
     
  13. macrumors regular

    Joined:
    Mar 5, 2004
    #13
    Yes, take a look at LibComponentLogging.
     
  14. macrumors 6502

    Joined:
    Jan 15, 2008
    Location:
    Atlanta
    #14
    Use Log4Cocoa

    http://log4cocoa.sourceforge.net/

    You can't pre-compile. I add the source code to my projects, but don't copy them into the folder structure. It works as advertised.
     

Share This Page