screepers / screeps-typescript-starter

Starter kit for TypeScript-based Screeps AI codes.
https://screepers.gitbook.io/screeps-typescript-starter/
The Unlicense
441 stars 318 forks source link

Anyway to conditionally set log level based on gulp build target (and other consts)? #42

Closed bryanbecker closed 7 years ago

bryanbecker commented 7 years ago

It would be nice to be able to conditionally set variables based on build environment.

I can think of a couple hypothetical ways to accomplish this:

resir014 commented 7 years ago

Not sure if this is what you're looking for, but I usually add the following to my config.ts so that I can have better control of my log levels:

// ./config/config.ts
export const ENABLE_DEBUG_MODE: boolean = false;
// ./main.ts
export function loop() {
  if (Config.ENABLE_DEBUG_MODE) {
    log.debug("Game loop started");
  }

  // ...
}

You might also be able to pass a NODE_ENV variable while you compile your code. A package like cross-env is a good cross-platform solution.

bryanbecker commented 7 years ago

I think you can can achieve what you posted above easier, and with less calls by just setting

// ./config/config.ts
export const LOG_LEVEL: number = LogLevels.DEBUG; // or ERROR, WARNING, INFO

Then you don't need to check if (Config.ENABLE_DEBUG_MODE) since the included log module does that for you. (i.e. if you set LOG_LEVEL to WARNING, then places in your code callign log.debug won't output)

What I was hoping for was a way to modify LOG_LEVEL based on the target. For example, npm run prod would set LOG_LEVEL to ERROR, while npm run dev would set it to DEBUG

ezolenko commented 7 years ago

You could look at how LOG_VSC variable is set and do something like that for LOG_LEVEL. Look for @@_repo_@@ in gulpfile.

I find it is more useful to modify log level at runtime (using log.level = in console) though, because I have way too much spam at debug level. :)

bryanbecker commented 7 years ago

I find it is more useful to modify log level at runtime (using log.level = in console)

I don't know why I didn't think of that :man_facepalming: ... that's way more convenient than mucking around with gulp

bryanbecker commented 7 years ago

I'm going to close this, as the above solution works just fine