Skip to content
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

Error when adding decorator to class #12

Open
nrfribeiro opened this issue Apr 25, 2020 · 7 comments
Open

Error when adding decorator to class #12

nrfribeiro opened this issue Apr 25, 2020 · 7 comments

Comments

@nrfribeiro
Copy link

Hi,
I'am having a error when executing my code after adding the decorator to a class.

! TypeError: Reflect.defineMetadata is not a function
at D:\Wiredkode\Projects\Covid19NeuroTracking\Functions\functions\node_modules\class-logger\dist\src\log-class.decorator.js:7:13
at __decorate (D:\Wiredkode\Projects\Covid19NeuroTracking\Functions\functions\lib\controlers\UsersController.js:5:95)
at Object. (D:\Wiredkode\Projects\Covid19NeuroTracking\Functions\functions\lib\controlers\UsersController.js:20:5)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object. (D:\Wiredkode\Projects\Covid19NeuroTracking\Functions\functions\lib\express\endpoints\post_sysadmins.js:5:27)
! We were unable to load your functions code. (see above)

  • It appears your code is written in Typescript, which must be compiled before emulation.
  • You may be able to run "npm run build" in your functions directory to resolve this.
@aigoncharov
Copy link
Owner

aigoncharov commented Apr 26, 2020

@nrfribeiro have installed reflect-metadata? See p.1 of the installation guide

@nrfribeiro
Copy link
Author

Hi,
It was my mistake. Is working :) thanks.

Now I have another question.
Following the thread #1 is there any way to enable logging at runtime, meaning depending on the log level that I have configured on Winston, I want to fully disable class-logger. I have an envvar that I use to change Winston log level, and based on that I want to decide to fully activate on not class.logger.

@aigoncharov
Copy link
Owner

@nrfribeiro I left in that thread a comment with the example. Would it work for you?

@nrfribeiro
Copy link
Author

Hi, I don't think so.
I want to enable or to disable the log at runtime. The example you provided only works at the first time the class is instantiated/accessed. Unfortunately, I have a few singleton classes, and I would like to enable or disable the log at runtime

@aigoncharov
Copy link
Owner

@nrfribeiro the same principle applies

import { LogClass as LogClassOriginal, IClassLoggerConfig } from 'class-logger'

let enabled: boolean = true
export const enable = () => { enabled = true }
export const disable = () => { enabled = false }


export const LogClass = (config: IClassLoggerConfig) => {
  if (!enabled) {
    return () => undefined // return an empty decorator placeholder
  }
  return LogClassOriginal(config)
} 

@nrfribeiro
Copy link
Author

nrfribeiro commented Apr 26, 2020

Hi, Thanks for your reply.
Imagine that I have a singleton class, which is only instantiated once.
When the app starts I have debug level set up for info. In that case the "my" LogClass will return undefined. Then, and without rebooting the app, I change log level to debug. Since my class already was instanciated and a LogClass assign, It would be impossible to enable logging (return LogClassOriginal)

@aigoncharov
Copy link
Owner

aigoncharov commented Apr 27, 2020

@nrfribeiro got it. In this case, it's not possible to completely disable the proxy creation, but you can make it not log anything,
See the global config section of the manual.
Whenever you want to disable the logging override log and logError

import { setConfig } from 'class-logger'

setConfig({
  log: () => {},
  logError: () => {}
})

This way you'll still have a wrapping proxy and you'll pay a performance penalty for that. However, I expect 99% of the apps not even noticing the performance impact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants