Comments (7)
+1 I need this functionality as well.
from js-logger.
Hey Guys, could you please provide a little more detail on what feature you are after? Would pulling the default
logHandler out into a member property be sufficient, ie:
Logger.defaultHandler = function (messages, context) { ... };
from js-logger.
My use case is to customize the message that gets printed out. For example, I always want the project name to be prepended to each log statement.
[MyProject] This is log statement
[MyProject] [ModuleA] This is a log statement from a named logger
@jonnyreeves For my case, I think it would be better if the message handling part of exposed instead:
// Prepend the logger's name to the log message for easy identification.
if (context.name) {
messages[0] = "[" + context.name + "] " + messages[0];
}
from js-logger.
So my current train of thought is that useDefaults
could be made a little more flexible when it comes to how the log messages are actually formatted - this would probably solve 80% of use-cases. To enable this, I am thinking of creating the following API:
Logger.useDefaults = function(options) {
options = merge(options, {
// Messages under this log level will not be written.
defaultLevel: Logger.DEBUG,
// Specify the format of log messages written to the console. This string should be
// made up using the following tokens:
// {{name}} The name of the logger.
// {{message}} The log message(s).
// {{timestamp}} timestamp when the log message is emitted
// {{level}} logLevel of the message as a string (ie: INFO).
logFormat: "[{{name}}] {{message}}",
// Will be invoked whenever a timestamp is required by the logFormat.
formatTimestamp: function () { return new Date.toString() },
});
So in @khirakawa's example, he could use:
Logger.useDefaults({
logFormat: "[MyProject] [{{name}}] {{messages}}"
});
I would expose the logFormatter on Logger.utils.formatter
so other, more bespoke log handlers, can also leverage it.
I'll try and knock some code together over the next few days and put it up for review, please let me know your thoughts if you have an opinion :)
from js-logger.
@jonnyreeves yes, that would work perfectly for my use case. Thanks!
from js-logger.
@jonnyreeves I'm also using js-logger to log on the client and server and I'd like to know if you're going to include the addition from @khirakawa
from js-logger.
Fixed in v1.2.0; you can now supplied a formatter
function to useDefaults
and mutate the messages
Array as you see fit.
from js-logger.
Related Issues (20)
- setLevel(String) HOT 2
- Request for giving access to time variable in Logger.timeEnd() HOT 1
- Formatting example breaks console substition HOT 5
- Please packed a esm version, standalone file. HOT 1
- setLevel doesn't seem to work HOT 6
- How to remove stacktrace from trace logs? HOT 1
- Uncaught TypeError: Cannot read properties of undefined (reading 'Logger')
- export ILogger interface HOT 2
- Logging into plain text files HOT 1
- Debug Level Not Showing Up HOT 1
- Migrate away from gulp HOT 1
- Request for tables HOT 1
- No DEMO link in readme HOT 3
- npm package 1.4.1 has different code from github HOT 1
- The 1.5.0 version does not work with typescript 3.1.3 (EDIT 1.4.1 works) HOT 5
- Add missing TRACE debug level in ts definitions
- Request for api to set log-level of the Profile calls HOT 1
- When logging the line number comes from the logger instead the file where the logger is used HOT 1
- How do I extend the logger? HOT 1
- yarn? 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 js-logger.