Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MorphingDragon

macrumors 603
Mar 27, 2009
5,160
6
The World Inbetween
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.

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

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 68000
Feb 11, 2010
1,554
0
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

macrumors 6502a
Oct 13, 2007
534
1
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.
 
Last edited:

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.

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

macrumors 6502a
Oct 13, 2007
534
1
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.
 

sivaprakash

macrumors member
Original poster
Mar 10, 2011
52
0
Chennai, India
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");
}
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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");
}

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

macrumors member
Original poster
Mar 10, 2011
52
0
Chennai, India
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

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.