Giter Club home page Giter Club logo

Comments (9)

moltar avatar moltar commented on May 27, 2024 1

I like the idea for "debug" capability.

But I think having to add explain to every call is a bit verbose. On the other hand does allow for granularity, in case we report any secrets.

I'm still leaving towards the global logger approach though.

from env-var.

evanshortiss avatar evanshortiss commented on May 27, 2024 1

On balance I think the debug module is best here.

It's common practice in the node community and it keeps the API of this module small.

I do like the proposed format of messages from above comments:

Reading RETRIES from environment : not found, using default of 5 (number)
Reading VTE_VERSION from environment : 5202 (string)

I'd be happy to accept a PR if anyone's interested in it. I can get around to it in a week or so if not.

from env-var.

evanshortiss avatar evanshortiss commented on May 27, 2024

I like this idea! I had considered adding some logging with the debug module before, but decided against it since I didn't want to add a dependency - your solution here is a good way to optionally support it 👍

In terms of the implementation, how about making it work similar to required and convertFromBase64? Those functions set boolean flags (see here) and those are used in the function (here) when reading the variable - you could just store the log function instead of a boolean.

It could default to a noop let out = () => {} , and explain(out) would set it to a real logger to do the logging?

from env-var.

evanshortiss avatar evanshortiss commented on May 27, 2024

Or as you say, default it to console.debug. We might just want to prefix logs with env-var so they're easy to search.

from env-var.

Brigadier1-9 avatar Brigadier1-9 commented on May 27, 2024

Great! Thanks for the steer, that was a big help. Having dabbled a little more, I was wondering if a slightly different approach would be better.

The issue (having thought about it) with the chained 'explain' is that it would be easy to miss - what if, somewhere fifty lines down, one of my team forgot to put '.explain' on a .get call. There would always be that niggling doubt in the back of my mind: "Am I really seeing all the environment vars that are in use here?"

Instead, I thought maybe adding a param to the 'from' constructor, something like:

const from = (container, extraAccessors, explainer) => {

then handle it in variable.js:

module.exports = function getVariableAccessors (container, varName, extraAccessors, explainer) {
  let doExplain = (typeof explainer === 'function')

then using 'doExplain' to conditionally add in the message construction and finally output it to the explainer function

This would avoid the need to call '.explain', with the (slight) tradeoff that the require now would look like:

const env = require('env-var).from(process.env,{}, console.debug)

Resulting in output more like this:

o.get('RETRIES').default('5').asInt()
env-var|Reading RETRIES from environment : not found, using default of 5 (number)
o.get('VTE_VERSION').default('5150').asInt()
env-var|Reading VTE_VERSION from environment : 5202 (number)
o.get('VTE_VERSION').default('5').asString()
env-var|Reading VTE_VERSION from environment : 5202 (string)

from env-var.

Brigadier1-9 avatar Brigadier1-9 commented on May 27, 2024

And one final thought - what if I use a couple of environment vars to optionally control the behaviour - for example:
ENV_VAR_DEBUG=true
ENV_VAR_TRUNC=30

The first is intended to enable debug mode (defaulting to console.debug), so no need for the developers to invoke the complex constructor - but the ops team can still enable if they need to see what is going on, without having to fiddle the code

The second because it occurred to me that some of the retrieved values may be quite long and it may be preferable to trunc the values upon output (using an ellipses to indicate truncation)

from env-var.

evanshortiss avatar evanshortiss commented on May 27, 2024

I thought maybe adding a param to the 'from' constructor, something like

Yeah, I like this. It's like a global on/off switch. Initially I was opposed to the ENV_VAR_DEBUG, but I think this is a great use case - no need for the developers to invoke the complex constructor - but the ops team can still enable if they need to

I'd rather avoid ENV_VAR_TRUNC though. Developers can enforce that in the explainer they pass if necessary.

So end result is something like this perhaps since it's a non-breaking change?

const { from, get } = require('env-var')

// I think varname might be useful to have passed to the explainer?
const env = from(process.env, {}, (varname, str) => {
  if (get('ENV_VAR_DEBUG').asBoolean() === true) {
    console.log(str)
  }
})

Alternatively this might be cleaner, but is a major version bump:

const { from, get } = require('env-var')

const env = from(process.env, {
  extraAccessors: {},
  explainer: (varname, str) => {
    if (get('ENV_VAR_DEBUG').asBoolean() === true) {
      console.log(str)
    }
  }
})

from env-var.

evanshortiss avatar evanshortiss commented on May 27, 2024

@Brigadier1-9 I just realised my example uses ENV_VAR_DEBUG too, but I agree with your idea that we can implement that logic internally 😄

Do you want to work on a PR for this?

from env-var.

evanshortiss avatar evanshortiss commented on May 27, 2024

@Brigadier1-9 I got around to an implementation of this that I think satisfies your requirements. Could you take a look at the 6.2.0 branch? The TLDR is that a logger can now be passed to from() to enable logging support.

from env-var.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.