2
lopu
193d

wait..... can you auto indent console logs depending on their nesting in functions?

I just realised it can be hard to read console logs because say you do

log('here 1')

callFunction()

log ('here 2')

But callFunction does a bunch of logging, then your here1 and here2 become separated !

But if you could make console log automatically add a couple spaces for every level of nesting/scoping that would be ideal .. ? 👀

Comments
  • 4
    Yes, but you'll need more than just console.log.

    I see two ways you can go forward with this.

    1. Write a "tap" HOF that you'll hook to functions. It will overshadow the global console.log inside the function it's tapping, generating a different indentation and another, new, "tap" HOF for tapping functions nested within the first one.

    2. Just use console.trace
  • 2
    Potentially it would be very easy to do with language that support Aspect programming like Java with AspectJ

    You could globally wrap every method call and increase logger indent on entry and decrease on exit

    I think even in python you could hack it together but not sure (possibly decorators would work well for this too)
  • 2
    ⚠️ Warning: solution 1 will drive typescript mad. To avoid that, you must override it's structural typing by using a class holding a "tap" HOF-method instead of just a function
  • 1
    seems like reinventing shell's DEBUG mode (-x).

    IDK, I kind of like the java's approach, i.e. a logger with configurable fields, some of which can be class name, method name, LoC, filename, file path, etc.

    The bash's approach - not so much. I mean, it's much easier than w/o those subshell +++'es, but an indentation does not say which function was called, only that it's been called.
  • 3
    @netikras indeed , that's what JavaScript's all about reinventing the wheel every at every single fucking step 🤌🏻
  • 1
    If you're in browser you always have console.group or whatever it's called 😄 kinda helpful sometimes msybe
  • 3
    You could do something like that

    ```js

    function getCallstackDepth() {

    const old = Error.stackTraceLimit;

    Error.stackTraceLimit = 100; // adjust as needed.

    return new Error().stack.split('\n').length

    Error.stackTraceLimit = old;

    }

    let lastDepth = getCallstackDepth() + 1;

    function log(...args) {

    const d = getCallstackDepth();

    if (d > lastDepth) {

    console.group();

    } else if (d < lastDepth) {

    console.groupEnd();

    }

    lastDepth = d;

    console.log(...args);

    }

    function foo() {

    log("foo");

    ignore();

    log("foo");

    }

    function ignore() {

    bar();

    }

    function bar() {

    log("baz", "me", 69);

    }

    log("root");

    foo();

    log("root");

    ```
  • 0
    @kobenz your post could possibly be null

    Typescript is good but at the same time pisses me off when I have to play typescript chess. I hate having to do hacky shit to make it happy
  • 1
    @TeachMeCode no hack, using classes to force nominal typing is a suggestion from the language's documentation.

    ---

    It works but adds unnecessary overhead, which to a control freak like me is like dropping a bucketful of acid on my brain
  • 1
    @rol1510 nerd. Fucking awesome btw!
  • 1
    @ScriptCoded I wanted to say this. Good call
  • 2
    @ScriptCoded @Ranchonyx Wait ?! THIS IS LITERALLY WHAT I'M TALKING ABOUT.

    AMAZING 🥰🥰🥰🥰🥰🥰🥰🥰
Add Comment