@@ -39,8 +39,91 @@ const ACCEPTABLE = {
3939} ;
4040
4141
42+ /**
43+ * Load logging configuration from environment variables or config file
44+ * Environment variables take precedence over config.js values
45+ * Format: COUNTLY_SETTINGS__LOGS__<KEY> where KEY is DEBUG, INFO, WARN, ERROR, DEFAULT, or PRETTYPRINT
46+ * @returns {Object } Logging configuration object with debug, info, warn, error, default, and prettyPrint properties
47+ */
48+ function loadLoggingConfig ( ) {
49+ // Start with config.js values
50+ let prefs = require ( '../config.js' , 'dont-enclose' ) . logging || { } ;
51+
52+ // Check for environment variable overrides
53+ const envDebug = process . env . COUNTLY_SETTINGS__LOGS__DEBUG ;
54+ const envInfo = process . env . COUNTLY_SETTINGS__LOGS__INFO ;
55+ const envWarn = process . env . COUNTLY_SETTINGS__LOGS__WARN ;
56+ const envError = process . env . COUNTLY_SETTINGS__LOGS__ERROR ;
57+ const envDefault = process . env . COUNTLY_SETTINGS__LOGS__DEFAULT ;
58+ const envPrettyPrint = process . env . COUNTLY_SETTINGS__LOGS__PRETTYPRINT ;
59+
60+ /**
61+ * Helper to parse environment variable as array
62+ * @param {string } envValue - Environment variable value
63+ * @returns {Array } Parsed array of module names
64+ */
65+ const parseEnvArray = ( envValue ) => {
66+ try {
67+ return JSON . parse ( envValue ) ;
68+ }
69+ catch ( e ) {
70+ return envValue . split ( ',' ) . map ( s => s . trim ( ) ) . filter ( s => s . length > 0 ) ;
71+ }
72+ } ;
73+
74+ /**
75+ * Helper to remove modules from other log levels to avoid conflicts
76+ * @param {Array } modules - Array of module names
77+ * @param {string } currentLevel - The level being set
78+ */
79+ const removeFromOtherLevels = ( modules , currentLevel ) => {
80+ const levels = [ 'debug' , 'info' , 'warn' , 'error' ] ;
81+ for ( let level of levels ) {
82+ if ( level !== currentLevel && Array . isArray ( prefs [ level ] ) ) {
83+ prefs [ level ] = prefs [ level ] . filter ( m => ! modules . includes ( m ) ) ;
84+ }
85+ }
86+ } ;
87+
88+ // Override with environment variables if present
89+ if ( envDebug !== undefined ) {
90+ prefs . debug = parseEnvArray ( envDebug ) ;
91+ removeFromOtherLevels ( prefs . debug , 'debug' ) ;
92+ }
93+
94+ if ( envInfo !== undefined ) {
95+ prefs . info = parseEnvArray ( envInfo ) ;
96+ removeFromOtherLevels ( prefs . info , 'info' ) ;
97+ }
98+
99+ if ( envWarn !== undefined ) {
100+ prefs . warn = parseEnvArray ( envWarn ) ;
101+ removeFromOtherLevels ( prefs . warn , 'warn' ) ;
102+ }
103+
104+ if ( envError !== undefined ) {
105+ prefs . error = parseEnvArray ( envError ) ;
106+ removeFromOtherLevels ( prefs . error , 'error' ) ;
107+ }
108+
109+ if ( envDefault !== undefined ) {
110+ prefs . default = envDefault ;
111+ }
112+
113+ if ( envPrettyPrint !== undefined ) {
114+ try {
115+ prefs . prettyPrint = JSON . parse ( envPrettyPrint ) ;
116+ }
117+ catch ( e ) {
118+ prefs . prettyPrint = envPrettyPrint === 'true' ;
119+ }
120+ }
121+
122+ return prefs ;
123+ }
124+
42125// Initialize configuration with defaults
43- let prefs = require ( '../config.js' , 'dont-enclose' ) . logging || { } ;
126+ let prefs = loadLoggingConfig ( ) ;
44127prefs . default = prefs . default || "warn" ;
45128let deflt = ( prefs && prefs . default ) ? prefs . default : 'error' ;
46129let prettyPrint = prefs . prettyPrint || false ;
@@ -140,11 +223,18 @@ const logLevel = function(name) {
140223 return prefs ;
141224 }
142225 else {
143- for ( let level in prefs ) {
226+ // Check log levels in priority order (most verbose first)
227+ const validLevels = [ 'debug' , 'info' , 'warn' , 'error' ] ;
228+
229+ for ( let level of validLevels ) {
230+ if ( ! prefs [ level ] ) {
231+ continue ;
232+ }
233+
144234 if ( typeof prefs [ level ] === 'string' && name . indexOf ( prefs [ level ] ) === 0 ) {
145235 return level ;
146236 }
147- if ( typeof prefs [ level ] === 'object' && prefs [ level ] . length ) {
237+ if ( typeof prefs [ level ] === 'object' && Array . isArray ( prefs [ level ] ) && prefs [ level ] . length ) {
148238 for ( let i = prefs [ level ] . length - 1 ; i >= 0 ; i -- ) {
149239 let opt = prefs [ level ] [ i ] ;
150240 if ( opt === name || name . indexOf ( opt ) === 0 ) {
@@ -333,7 +423,14 @@ const setPrettyPrint = function(enabled) {
333423 * @returns {string } The current log level
334424 */
335425const getLevel = function ( module ) {
336- return levels [ module ] || deflt ;
426+ if ( module ) {
427+ // If not in cache, compute it from config
428+ if ( ! ( module in levels ) ) {
429+ return logLevel ( module ) ;
430+ }
431+ return levels [ module ] ;
432+ }
433+ return deflt ;
337434} ;
338435
339436/**
0 commit comments