-
Notifications
You must be signed in to change notification settings - Fork 4
Implement a possibility to create multiple logger objects #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you @MarcSchaetz for brining this up as a suggestion. I think it's right to put it into it's own issue, I just thought it was related before. Here are my further findings in regards to the Originally posted: #1 (comment) Further findings: This time it actually worked 🙈. The logger is now accessible outside of components and can be loaded by simply doing Please note again, I am not proficient in Typescript so this is probably "ugly" for some 🙈. Here is the function I added to the public createLogger(options: ILoggerOptions) {
options = Object.assign(this.getDefaultOptions(), options);
if (this.isValidOptions(options, this.logLevels)) {
const $log = this.initLoggerInstance(options, this.logLevels);
$log.install = function (Vue: any, options: ILoggerOptions) {
Vue.provide('vuejs3-logger', $log);
Vue.config.globalProperties.$log = $log;
}
return $log
} else {
throw new Error(this.errorMessage);
}
} And below is my I have not found a way to directly export the import VueLogger from 'vuejs3-logger/src'
const isProduction = process.env.NODE_ENV === 'production'
export const options = {
isEnabled: true,
logLevel: isProduction ? 'error' : 'debug',
stringifyArguments: false,
showLogLevel: true,
showMethodName: true,
separator: ':',
showConsoleColors: true,
}
const loggerCreated = VueLogger.createLogger(options)
export const logger = loggerCreated
export const log = loggerCreated |
@chrisspiegl As you mentioned, the feature of adding different loggers is very common in programming. The implementation would be easy if I just added another loggerinstance with a different key. createLogger(key: string, options: ILoggerOptions) {
// Create logger instance
Vue.provide(key, loggerInstance);
} It then is possible to inject the logger into different components, classes, etc. However the question is if this is desirable. Another approach would be a class SomeClass {
logger: Log
constructor() {
this.logger = createLogger({});
}
] |
Hi @MarcSchaetz, I think there are two separate things going on:
1: Multiple LoggersThis would indeed be something that could be of interest as well, despite being my goal. Specifically for the purpose of namespacing loggers so they show more than just the function they were invoked out of. 2: Getting the Logger InstanceThis is what I am after and what I think I achieved with the code shared above. The I hope my description is good and understandable? I am currently only after the 'injecting the one and only logger into all kinds of files 👍 . |
Thank for you all 👍! I've been working on providing a logger instance to an external js file for a couple of days. This really helps me a lot. |
Any news on this? |
@54mu3l Yes. Due to me not really working with Vue in the past, I simply forgot about the logger. I looked at it again and I'm optimistic on providing some update at the weekend. |
@54mu3l @chrisspiegl Please checkout commit 5b2dfd1 and give me your opinion on it. |
Hi @MarcSchaetz, I saw the change however at this point in time I am not actively coding on any of my projects where I am using VUE. When I get back to that (I do not have a timeline at this point) I will also check this out. |
@MarcSchaetz Thank you so much for your great work! I had a look at your commit and checked out the project. At a first glance this looks good to me... I think the best way for me to test this, would be within my vue project. Could you publish this branch as a Thank you! |
Hi @54mu3l I just released the next tag on npm. |
Hi @MarcSchaetz Great, thank you! I just did some tests: First of all there seems to be a problem with the types in this release. I needed to manually move After fixing this, I tried to use Until now my code looked like this (in case you're wondering, I'm using quasar = just ignore the import { boot } from 'quasar/wrappers';
import { VueLogger } from 'vuejs3-logger';
const isProduction = process.env.NODE_ENV !== 'development';
const options = {
isEnabled: true,
logLevel: isProduction ? 'error' : 'debug',
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: '|',
showConsoleColors: false,
};
export default boot(({ app }) => {
app.use(VueLogger, options);
}); Now my new code looks like this: import { boot } from 'quasar/wrappers';
import { createLogger } from 'vuejs3-logger';
const isProduction = process.env.NODE_ENV !== 'development';
const options = {
isEnabled: true,
logLevel: isProduction ? 'error' : 'debug',
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: '|',
showConsoleColors: false,
};
const logger = createLogger(options);
export default boot(({ app }) => {
app.use(logger);
});
export { logger }; The last line enables me to import the logger instance into other non-vue-js-files. (= exactly what I want 🥳) ==> There is only one big problem (using the code above): within a vue component I used this temporary workaround to get it to work: import { boot } from 'quasar/wrappers';
import { createLogger } from 'vuejs3-logger';
const isProduction = process.env.NODE_ENV !== 'development';
const options = {
isEnabled: true,
logLevel: isProduction ? 'error' : 'debug',
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: '|',
showConsoleColors: false,
};
const logger = createLogger(options);
export default boot(({ app }) => {
// app.use(logger);
app.use(VueLogger, options);
});
export { logger }; With this example I used two logger instances. One for within vue (with Btw this is how I'm doing the same thing with i18n: import { boot } from 'quasar/wrappers';
import { createI18n } from 'vue-i18n';
import messages from 'src/i18n';
const i18n = createI18n({
legacy: false,
locale: 'en',
messages,
});
export default boot(({ app }) => {
// Set i18n instance on app
app.use(i18n);
});
export { i18n }; |
Hey @54mu3l Your workaround is actually the preferred way to use it as far as I implemented it. const options = {
isEnabled: true,
logLevel: isProduction ? 'error' : 'debug',
stringifyArguments: false,
showLogLevel: false,
showMethodName: false,
separator: '|',
showConsoleColors: false,
};
export default boot(({ app }) => {
// This line internally calls createLogger(options) but also registers
// the logger with the key "vuejs3-logger" inside the Vue instance
app.use(VueLogger, options);
});
// This line creates a whole new local logger object which is not
// registered in the Vue instance
const logger = createLogger(options);
export { logger }; What you however can do is use the new static app.use(VueLogger.register("your-key"), options); It must be called inside the |
Hi @MarcSchaetz Alright, thank you! I just did some more testing and this works perfectly for me. Thanks |
ciao, any news on the release of this new features? i see version 2.0.0 on the repository that is not yet release on master |
Uh oh!
There was an error while loading. Please reload this page.
Suggested by @chrisspiegl
Basically, the behavior of other plugins nowadays seems to be to provide a
createLogger
function as a named export.This
createLogger
then builds the logger and also has a keyinstall
which would be used by theapp.use
to call with theapp
and possibleoptions
.All this could then be used by the user in a
plugins/logger.js
to build their own logger which has the same preferences inside components and outside.In the mean time, I basically did the same for myself with a bit of a workaround (wrapping the
vuejs3-logger
in it's own plugin createLogger 🙈.Maybe the code below will be helpful (it's workaround):
The improvement with official support of a
createLogger
function would solve all the issues and would make the logger available upon first call and not just after it actually was initiated withapp.use
.Again, hope this helps and am looking forward to an update to this plugin 🌸.
Oh… and P.S.: hi from Germany 👍 just a few hundred km north from you 🚗 .
Originally posted by @chrisspiegl in #1 (comment)
The text was updated successfully, but these errors were encountered: