Comments (4)
I would like to disable it because it's useless for our Electron.js app. It's a very small slow down for every getLogger()
call (and I use getLogger()
often.)
May be you think - hey, the slow down is negligible, why do you care? My reply is: why not to care? Why to have some code reading localStore and cookie which I do not need? IMO a good logging library should do just logging and everything else should be optional (or when default, then possible to opt-out.) And loglevel is almost like that except the persistence 😀
I understand it's not possible to change it for the current API. So I suggest:
- What about to have a possibility to disable/change it for the
getLogger()
calls at least? - Think of it when doing any Loglevel 2.0 #119
from loglevel.
Hi @xmedeko. Could you tell me more about why you need to disable this?
Right now, persistence is fully configurable at level configuration time - you can pass an option to setLevel
to control this. If you never call that with persistence enabled, then nothing will ever be stored, and getPersistedLevel
will never do anything, and has no impact. Is there a reason that doesn't work for you?
Unfortunately, if it is a problem, it would be difficult to disable persistence at load time. By design loglevel is immediately initialized and available when imported, with zero options or construction required, to make it as easy as possible to quickly use. That means that getPersistedLevel
is already called before the point where you could configure it.
from loglevel.
It's not called every time you call getLogger
, it's called once for each different logger that you use. If you pass the same name twice, it's only called the first time around:
Lines 247 to 252 in 342f103
I've done a little digging, and as far as I can tell in practice you'd have to instantiate many thousands of distinct logger instances before the local storage check has even 1ms total impact. If you're doing that much logging frequently enough that this has a measurable impact (e.g. creating 1000 loggers every second, each with distinct keys) then it's likely that you're generating huge amounts of logs, and that will be significantly more expensive than any localstorage lookups.
That said, if you're committed to trying to avoid this regardless:
- It is possible to disable persistence in all subloggers today, by using Symbol keys for your logger names instead of strings. Symbol keys by definition aren't comparable between page loads, so they can never be meaningfully persisted, and all persistence is skipped:
Lines 125 to 127 in 342f103
- Alternatively, I'd be open to providing options to
getLogger
somehow, e.g. with a{ persistence: false }
option. I don't agree that this is sensible for anybody from a performance perspective, but it seems like that might be a useful feature for other use cases anyway, so if you're keen on it you're welcome to investigate. Tricky question though: what happens if you call getLogger twice, with different options? If logger caching needs to deep compare the configured options objects then this is plausibly going to make getLogger more expensive for almost all use cases, not cheaper. Open to ideas there.
Honestly though, I don't think you should do any of this. LocalStorage is only expensive in comparison to bare object property lookups, loglevel is not optimized for performance to that degree at all, and unless you're working in some very specific high-performance environments the rest of your JS application is not either. There will be other much easier and more effective performance improvements in your code elsewhere!
If you are doing a quantity of logging where this truly matters, and you're really really sure that this practically affects your performance, imo you're better off rolling your own microlibrary entirely. This is a tiny lib and it has lots of small UX & cross-browser compatibility details you could skip entirely for a custom case. There's probably many other micro-optimizations and simplifications here that would save >1ms in such an application, e.g. if you're calling getLogger billions of times then I suspect removing the argument validation checks in getLogger that happen on every call would make a bigger practical difference (but changes like that doesn't make sense for loglevel itself, since there's a UX tradeoff).
from loglevel.
I just repeat my previous comment:
May be you think - hey, the slow down is negligible, why do you care? My reply is: why not to care? Why to have some code reading localStore and cookie which I do not need?
Instead of getLogger({ persistence: false })
I would welcome some configuration like methodFactory
- e.g.:
require('loglevel').persistence = {
getLevel(loggerName) { ... },
setLevel(loggerName, levelName) { ... }
}
Which would be more versatile.
This is not a blocker for me, just IMO such things should be configurable. And, of course, I can always do copy&paste a remove that code.
from loglevel.
Related Issues (20)
- Keep source(file:line) remain the same when methodFactory is been called. HOT 4
- log is not defined when attempting setLevel() in console HOT 3
- Programmatic log levels HOT 1
- methodFactory logLevel parametr has always same value HOT 2
- Show filename and line of logging code HOT 2
- Would you consider deprecating in favor of Roarr? HOT 2
- loglevel print duplicate log in React component HOT 5
- Is it possible to import Loglevel as an ES module in client-side javascript? HOT 5
- Possibility to save log output to file HOT 4
- current npm web page for loglevel shows no documentation at all HOT 3
- LogLevel output doesn't show when running in a test HOT 11
- LogLevel `trace`, `debug` and `info` not shown anymore on Google Chrome HOT 2
- Log to a file HOT 1
- Feature/Enhancement: add a level between 'debug' and 'trace' HOT 1
- Is it possible to enable multiple plugins? HOT 2
- how to send to logs server? HOT 1
- Couldnt locate documention or plugins to format logged payload as JSON (compatible with Datadog etc) HOT 2
- Why do I have to configure the log in every typescript file? HOT 12
- The name of the file the log has been generated and the line number is broken when using a plugin HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loglevel.