simple-leveled-log-methods
Version:
a simple and opinionated logging library. plays well with aws lambda + cloudwatch.
38 lines • 1.99 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRecommendedMinimalLogLevelForEnvironment = void 0;
const constants_1 = require("./constants");
const identifyEnvironment_1 = require("./identifyEnvironment");
const getLogLevelFromEnvVar = () => {
// if LOG_LEVEL was specified, use that
const logLevelEnvVar = process.env.LOG_LEVEL || null; // cast falsy values to null
if (logLevelEnvVar) {
// if the log level is valid, use it
if ((0, constants_1.isOfLogLevel)(logLevelEnvVar))
return logLevelEnvVar;
// otherwise, warn and continue to attempt other options
console.warn(`environmental variable LOG_LEVEL was set to an invalid value: '${logLevelEnvVar}'. using the default instead`);
}
// if LOG_DEBUG was specified as true, use that (it's a common intuitive alias)
const logDebugEnvVar = process.env.LOG_DEBUG || null;
if (logDebugEnvVar === 'true')
return constants_1.LogLevel.DEBUG;
// todo: consider supporting other LOG_${level} options
// otherwise, null
return null;
};
const getRecommendedMinimalLogLevelForEnvironment = () => {
// if the LOG_LEVEL env var is defined, then use what is specified by that
const logLevelFromEnvVar = getLogLevelFromEnvVar();
if (logLevelFromEnvVar)
return logLevelFromEnvVar;
// identify the env we're in
const env = (0, identifyEnvironment_1.identifyEnvironment)();
// if we're in aws lambda env, then default to DEBUG - since cloudwatch costs are cheap and extra visibility is worth it in most cases
if (env === constants_1.SupportedEnvironment.AWS_LAMBDA)
return constants_1.LogLevel.DEBUG;
// otherwise, default to INFO - to balance signal -vs- noise
return constants_1.LogLevel.INFO;
};
exports.getRecommendedMinimalLogLevelForEnvironment = getRecommendedMinimalLogLevelForEnvironment;
//# sourceMappingURL=getRecommendedMinimalLogLevelForEnvironment.js.map
;