PDA

View Full Version : Logger for Objective-C




sivaprakash
May 9, 2011, 07:50 AM
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



MorphingDragon
May 9, 2011, 08:13 AM
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

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.


//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);

jiminaus
May 9, 2011, 08:22 AM
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.

lee1210
May 9, 2011, 08:45 AM
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

ulbador
May 9, 2011, 08:47 AM
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

Another +1 for log4. We use it in Java and it's great.

larkost
May 9, 2011, 10:41 PM
A while ago I wrote up a little article on improving on NSLog() (http://wranglingmacs.blogspot.com/2009/04/improving-on-nslog.html). 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.

lee1210
May 9, 2011, 11:05 PM
A while ago I wrote up a little article on improving on NSLog() (http://wranglingmacs.blogspot.com/2009/04/improving-on-nslog.html). 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.

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

larkost
May 9, 2011, 11:43 PM
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.

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 (http://boredzo.org/blog/archives/2008-01-19/next-week-apple-system-logger).

sivaprakash
May 11, 2011, 05:20 AM
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?



#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");
}

jiminaus
May 11, 2011, 05:57 AM
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?



#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");
}



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.

sivaprakash
May 11, 2011, 07:08 AM
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

jiminaus
May 11, 2011, 07:21 AM
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

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.

vocaro
May 11, 2011, 04:37 PM
Or any other better framework available out.

Yes, take a look at LibComponentLogging (http://0xc0.de/LibComponentLogging).

badmac78
May 11, 2011, 04:44 PM
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.