Logging Mechanisms in Cocoa

  • Hi All,

            I'm porting windows application to Mac using Cocoa. Now i need to
    implement the Logging ( NSLog/printf's with some trace levels ) in Cocoa.
    Can any one clarify my following queries.

            i) Are there any open source Logging Frameworks in Cocoa ?

            ii) If not , please provide some guidelines on implementing in it
    using Cocoa/

            Please help me to make a progress in this. Thanks in Advance.

    -JanakiRam.
  • There is an open source port of Log4j called Log4Cocoa.

    It's maintained by Timothy Reaves and you can find it here:
    http://log4cocoa.sourceforge.net/

    It's not a complete port yet ‹ some features are missing, but it's
    definitely usable.

    Eric

    Le 08-02-07 14:15, « JanakiRam » <johnyatforums...> a écrit :

    > Hi All,
    >
    > I'm porting windows application to Mac using Cocoa. Now i need to
    > implement the Logging ( NSLog/printf's with some trace levels ) in Cocoa.
    > Can any one clarify my following queries.
    >
    > i) Are there any open source Logging Frameworks in Cocoa ?
    >
    > ii) If not , please provide some guidelines on implementing in it
    > using Cocoa/
    >
    > Please help me to make a progress in this. Thanks in Advance.
    >
    > -JanakiRam.

    --
    Eric


    Éric Trépanier
    Senior Software Developer, Service Delivery Platform
    Radialpoint Inc.
    Phone: +1 (514) 286-2636 x 2669
    <eric.trepanier...>
    www.radialpoint.com <http://www.radialpoint.com/>
  • On 2/8/08 12:45 AM, JanakiRam said:

    > I'm porting windows application to Mac using Cocoa. Now i need to
    > implement the Logging ( NSLog/printf's with some trace levels ) in Cocoa.

    'man asl'

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • On Feb 7, 2008, at 21:15, JanakiRam wrote:
    > I'm porting windows application to Mac using Cocoa. Now i
    > need to
    > implement the Logging ( NSLog/printf's with some trace levels ) in
    > Cocoa.
    > Can any one clarify my following queries.
    >
    > i) Are there any open source Logging Frameworks in Cocoa ?
    >
    > ii) If not , please provide some guidelines on implementing
    > in it
    > using Cocoa/

    The simplest solution: use NSLog.

    Little more fancy - wrap NSLog in macros:

    #ifdef DEBUG
        #define Debug(format, ...)  NSLog(@"<Debug>: " format,
    ##__VA_ARGS__)
    #else
        #define Debug(format, ...)
    #endif

    #define Warn(format, ...)  NSLog(@"<Warning>: " format, ##__VA_ARGS__)
    #define Error(format, ...)  NSLog(@"<Error>: " format, ##__VA_ARGS__)

    See also http://www.borkware.com/rants/agentm/mlog/

    Best Regards,

    Nir Soffer
  • Peter Hosey has blogged a lot recently about asl (Apple System Logger)

    http://boredzo.org/blog/archives/2008-01-20/why-asl

    there are about 9 blog posts covering this topic starting jan 20th of this year

    +Clint
  • On Feb 8, 2008, at 9:13 AM, Clint Shryock wrote:

    > Peter Hosey has blogged a lot recently about asl (Apple System Logger)
    >
    > http://boredzo.org/blog/archives/2008-01-20/why-asl
    >
    > there are about 9 blog posts covering this topic starting jan 20th
    > of this year
    >

    asl isn't any better than NSLog, even though that blog post claims it
    is.  In the Cocoa/ObjC world, this isn't overly surprising (that
    someone would think asl is really any different that syslog or
    NSLog).  Logging has meant more than just writing something to the
    system log file for a good long while, in most of the software
    development world.  And compile time manipulation of logon/logoff
    isn't adequate either.  Anyone who has ever used any of the Java
    logging frameworks has know - and used - this for a decade.

    If all you want is to be able to write to the system log file, with
    the plethora of other junk, then by all means uses asl or NSLog.  If
    you need modern logging, use log4cocoa.  It's not complete, and work
    specifically in the area of threading is needed, but it's being used
    in a good many enterprise production applications today.  It supportz
    the ability to have different classes logging at different levels,
    logging to application specific files or the system log file, the
    ability to use rolling file appenders, the ability to customize to a
    great deal the format of the log message, and this can all be done at
    compile or run time.
  • On 9 Feb '08, at 8:23 PM, Timothy Reaves wrote:

    > asl isn't any better than NSLog, even though that blog post claims
    > it is.

    Yes, it is. Instead of being dumped as text to stderr, the log entries
    get stored in a structured form that makes it easy to query based on
    things like the process name. ASL lets you add your own key/value
    metadata pairs, which the API can later query on (you can also use
    Console.app in Leopard to build custom queries. I found this really
    useful for making queries that would show only logs from my specific
    subsystems of framework, across all processes it runs in.)

    Being able to turn different log levels on/off is great, but that's
    not what ASL is about. ASL is just about storing structured, queryable
    data in the system log. The kind of stuff you're describing is
    orthogonal to that.

    —Jens
  • On Feb 10, 2008, at 2:18 AM, Jens Alfke wrote:

    >
    > On 9 Feb '08, at 8:23 PM, Timothy Reaves wrote:
    >
    >> asl isn't any better than NSLog, even though that blog post claims
    >> it is.
    >
    > Yes, it is. Instead of being dumped as text to stderr, the log
    > entries get stored in a structured form that makes it easy to query
    > based on things like the process name. ASL lets you add your own key/
    > value metadata pairs, which the API can later query on (you can also
    > use Console.app in Leopard to build custom queries. I found this
    > really useful for making queries that would show only logs from my
    > specific subsystems of framework, across all processes it runs in.)
    >
    > Being able to turn different log levels on/off is great, but that's
    > not what ASL is about. ASL is just about storing structured,
    > queryable data in the system log. The kind of stuff you're
    > describing is orthogonal to that.
    >
    > —Jens

    Not sure you've ever used NSLog() Jens.  By default, in a running
    application, it does indeed send to the system log file, not just
    stdout or stderr.  And a database is for "structured, queryable
    data".  Whereas a log file can be used for that, one in which other
    apps are dumping non-structured data is just going to pollute the
    issue.  Orthogonal?  That's a stretch, but if you say so.
  • On 10 Feb '08, at 5:42 PM, Timothy Reaves wrote:

    > Not sure you've ever used NSLog() Jens.

    Heh. I spent seven years writing Cocoa apps and frameworks at Apple.

    > By default, in a running application, it does indeed send to the
    > system log file, not just stdout or stderr.

    It's complicated. In 10.4 and earlier, NSLog literally did just write
    to stderr. When the system launched a new application, it connected
    stderr (and stdout) to the file "console.log". So the effect of NSLog
    was normally to write to console.log. (Unless you launched it from
    Xcode or a shell.)

    In Leopard, there is no more console.log. Instead, NSLog calls ASL.
    This writes to the ASL database and system.log. (Stderr output gets
    caught by launchd and echoed to ASL, though in kind of an ugly form.)

    > And a database is for "structured, queryable data".  Whereas a log
    > file can be used for that, one in which other apps are dumping non-
    > structured data is just going to pollute the issue.

    Not sure if you've ever used ASL, Tim. It has an honest-to-god
    structured data store, not a text file. And there are APIs to query
    it. It does echo a textual message to system.log, as a side effect,
    for backwards compatibility.

    How about you read the man page for asl(3) before we resume this fun
    discussion?

    —Jens
  • On 11 feb 2008, at 04:57, Jens Alfke wrote:

    > On 10 Feb '08, at 5:42 PM, Timothy Reaves wrote:
    >
    >> Not sure you've ever used NSLog() Jens.
    >
    > Heh. I spent seven years writing Cocoa apps and frameworks at Apple.

    This post then comes at the right moment and I hope your experience
    will provide the answer to the next question -- which might be of
    interest for others who like me start using asl. I was trying to log
    through asl (in XCode3, Leopard) with the statement

    asl_log(NULL, NULL, 1, "TESTING ASL-LOG:%s", "asl-log test");

    but I do not see anything resulting from it. Neither in the XCode
    console, nor in the Console app. Why not? I suppose I am doing
    something wrong, but what?

    Hans van der Meer
  • Use syslog -w option , which causes syslog to wait for new messages. For
    more info: man syslog

    - Apparao,
    Samsung Inc.

    On 2/11/08, Hans van der Meer <hansm...> wrote:
    >
    > asl_log(NULL, NULL, 1, "TESTING ASL-LOG:%s", "asl-log test");
    >
    > but I do not see anything resulting from it. Neither in the XCode
    > console, nor in the Console app. Why not? I suppose I am doing
    > something wrong, but what?
    >
  • On 11 Feb '08, at 12:53 AM, Hans van der Meer wrote:

    > asl_log(NULL, NULL, 1, "TESTING ASL-LOG:%s", "asl-log test");
    >
    > but I do not see anything resulting from it. Neither in the XCode
    > console, nor in the Console app. Why not? I suppose I am doing
    > something wrong, but what?

    Nothing should show up in the Xcode console, because ASL doesn't write
    to stderr. It would be nice if Xcode intercepted ASL output from tasks
    it runs and echoed it to its console; maybe someday. I got around this
    in my code by having my own logging macros that write to stderr
    instead of asl if stderr looks like it points to a TTY. (Sadly, I
    don't have that tricky code snippet anymore. I think I swiped most of
    it from the innards of NSLog.)

    Where in Console.app did you look? Show the log list (via the View
    menu) and select "All Messages" (the first item in the list). You
    should see it there.

    If you want to make a view in Console that will show only your app's
    messages, you can use "New Log Database Query..." and fill out the
    form. The most useful things to filter on are Sender (process name) or
    Facility (user-defined string you pass into asl_log.) Facility is
    useful if your code may run in multiple apps.

    —Jens