Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
![](/static/devrant/img/pipeless-devrant-banner-white.png)
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
kobenz779118dYes, 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 -
Hazarth9259117dPotentially 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) -
kobenz779117d⚠️ 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
-
netikras34950117dseems 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. -
kobenz779117d@netikras indeed , that's what JavaScript's all about reinventing the wheel every at every single fucking step 🤌🏻
-
ScriptCoded19182117dIf you're in browser you always have console.group or whatever it's called 😄 kinda helpful sometimes msybe
-
rol1510122117dYou 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");
``` -
TeachMeCode5147117d@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 -
kobenz779117d@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 -
lopu872116d
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 .. ? 👀
rant
console log
logging
debugging
scope
javascript