Skip to content

Commit

Permalink
Introduce improved logging detail
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Sep 29, 2022
1 parent ca68067 commit 7ba1c1c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/main.js
@@ -1,6 +1,9 @@
const fs = require('fs')
const path = require('path')
const os = require('os')
const packageJson = require('../package.json')

const version = packageJson.version

const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg

Expand Down Expand Up @@ -44,7 +47,11 @@ function parse (src) {
}

function _log (message) {
console.log(`[dotenv][DEBUG] ${message}`)
console.log(`[dotenv@${version}][INFO] ${message}`)
}

function _debug (message) {
console.log(`[dotenv@${version}][DEBUG] ${message}`)
}

function _resolveHome (envPath) {
Expand All @@ -71,6 +78,8 @@ function config (options) {
// Specifying an encoding returns a string instead of a buffer
const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }))

_log('Loading env from .env')

Object.keys(parsed).forEach(function (key) {
if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
process.env[key] = parsed[key]
Expand All @@ -81,9 +90,9 @@ function config (options) {

if (debug) {
if (override === true) {
_log(`"${key}" is already defined in \`process.env\` and WAS overwritten`)
_debug(`"${key}" is already defined in \`process.env\` and WAS overwritten`)
} else {
_log(`"${key}" is already defined in \`process.env\` and was NOT overwritten`)
_debug(`"${key}" is already defined in \`process.env\` and was NOT overwritten`)
}
}
}
Expand All @@ -92,7 +101,7 @@ function config (options) {
return { parsed }
} catch (e) {
if (debug) {
_log(`Failed to load ${dotenvPath} ${e.message}`)
_debug(`Failed to load ${dotenvPath} ${e.message}`)
}

return { error: e }
Expand Down

0 comments on commit 7ba1c1c

Please sign in to comment.