Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:39:41+00:00 2026-06-09T17:39:41+00:00

Instead of having to create a custom logger for TRACE, e.g. following what methods

  • 0

Instead of having to create a custom logger for TRACE, e.g. following what methods were called and what classes were instantiated, is there an easy way to make all methods under a class log themselves? This is for a node.js application.

class MyClass

  constructor: () ->
    console.log 'MyClass:constructor'

  doThat: () ->
    console.log 'MyClass:doThat'

exports.MyClass = MyClass

myClass = new MyClass()
myClass.doThat()

If I had my way, you’d see 4 log messages instead of 2 (thus having to write less code to trace what’s happening).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:39:42+00:00Added an answer on June 9, 2026 at 5:39 pm

    I recently needed to implement something like this to trace some complicated OO-recursive stuff. Basically, i wanted to make a method “traceable” without polluting it too much; so maybe the solution could be applied here too.

    First, add a function that makes other functions traceable:

    Function::trace = do ->
      makeTracing = (ctorName, fnName, fn) ->
        (args...) ->
          console.log "#{ctorName}:#{fnName}"
          fn.apply @, args
      (arg) ->
        for own name, fn of arg 
          @prototype[name] = makeTracing @name, name, fn
    

    Then, to use it, just add a @trace before each method you want to trace:

    class MyClass
      @trace methodA: ->
        @methodB 42
    
      @trace methodB: ->
        console.log "method b called with #{n}"
    

    Or add @trace only once and then indent all the traceable methods one more level:

    class MyClass
      @trace 
        methodA: ->
          @methodB 42
    
        methodB: (n) ->
          console.log "method b called with #{n}"
    

    As you can see trace is kind of abusing CoffeeScript’s syntax. method: -> 'foo' inside a class MyClass is interpreted a method definition. But @trace method: -> 'foo' is interpreted a calling the trace function of MyClass (which is a Function instance, to which we’ve added the trace function) passing it a literal object with one method key. In JavaScript it would be something like this.trace({method: function() {return 'foo';}}).

    The trace function will just take that object and iterate it’s keys (the method names) and values (the methods) and add functions to the MyClass prototype that log their calls and in turn call the original methods.

    Anyway, the output of (new MyClass).methodA() will be:

    MyClass:methodA
    MyClass:methodB
    method b called with 42
    

    This solution doesn’t work for constructors though, as they are not just normal methods.

    You can get quite fancy with this. You can also log the arguments passed to each method, the return value, and even add indentation for nested calls if you so desire (the resulting traces can then be very helpful if you need to debug a complex problem =D).


    Update: as a more interesting example, here’s a mini-version of the typical composite pattern example, geometrical figures and groups of figures: http://jsfiddle.net/2YuE7/ with a more interesting tracing function. All figures understand the move method. If we have this composite figure and call move on it:

    f = new Composite [
      new Rectangle 5, 10, 3, 4
      new Composite [
        new Circle 0, 0, 2
        new Circle 0, 0, 4
        new Circle 0, 0, 6
      ]
    ]
    
    f.move 2, 3
    

    The trace output is:

    (Composite[Rectangle[5,10,3,4],Composite[Circle[0,0,2],Circle[0,0,4],Circle[0,0,6]]]).move(2, 3)
    | (Rectangle[5,10,3,4]).move(2, 3)
    | -> Rectangle[7,13,3,4]
    | (Composite[Circle[0,0,2],Circle[0,0,4],Circle[0,0,6]]).move(2, 3)
    | | (Circle[0,0,2]).move(2, 3)
    | | -> Circle[2,3,2]
    | | (Circle[0,0,4]).move(2, 3)
    | | -> Circle[2,3,4]
    | | (Circle[0,0,6]).move(2, 3)
    | | -> Circle[2,3,6]
    | -> Composite[Circle[2,3,2],Circle[2,3,4],Circle[2,3,6]]
    -> Composite[Rectangle[7,13,3,4],Composite[Circle[2,3,2],Circle[2,3,4],Circle[2,3,6]]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to view the latest debug message instead of having to
Is it possible to make custom 404 pages for specific subdirectories instead of having
motivation: I would like to create a utility class so that instead of having
Instead of having to type tmux every time, how could I have tmux always
Instead of having nested callbacks in JS, I would like to fire and listen
Instead of having an add() method in the repository, I would like to overload
Constant height so just left/right images should be necessary instead of having 4+, right?
In compiler design, why instead of having a caller or callee register saving arrangement,
I'm having a problem where instead of reading a text file from the location
I know Apple is big on having you use NS objects instead of true

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.