@omen.foundation/node-microservice-runtime 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/logger.cjs CHANGED
@@ -38,15 +38,20 @@ const pino_1 = __importStar(require("pino"));
38
38
  const node_stream_1 = require("node:stream");
39
39
  const env_js_1 = require("./env.js");
40
40
  function isRunningInContainer() {
41
+ if (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
42
+ process.env.CONTAINER === 'beamable' ||
43
+ !!process.env.ECS_CONTAINER_METADATA_URI ||
44
+ !!process.env.KUBERNETES_SERVICE_HOST) {
45
+ return true;
46
+ }
47
+ if (process.env.NAME_PREFIX || process.env.ROUTING_KEY) {
48
+ return false;
49
+ }
41
50
  const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
42
- const hasNoRoutingKey = !process.env.NAME_PREFIX && !process.env.ROUTING_KEY;
43
- if (hasBeamableEnvVars && hasNoRoutingKey) {
51
+ if (hasBeamableEnvVars) {
44
52
  return true;
45
53
  }
46
- return (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
47
- process.env.CONTAINER === 'beamable' ||
48
- !!process.env.ECS_CONTAINER_METADATA_URI ||
49
- !!process.env.KUBERNETES_SERVICE_HOST);
54
+ return false;
50
55
  }
51
56
  function mapPinoLevelToBeamableLevel(level) {
52
57
  switch (level) {
@@ -164,19 +169,14 @@ function createLogger(env, options = {}) {
164
169
  }
165
170
  else {
166
171
  try {
167
- require.resolve('pino-pretty');
168
- return (0, pino_1.default)({
169
- ...pinoOptions,
170
- transport: {
171
- target: 'pino-pretty',
172
- options: {
173
- colorize: true,
174
- translateTime: 'HH:MM:ss.l',
175
- ignore: 'pid,hostname',
176
- singleLine: false,
177
- },
178
- },
179
- }, process.stdout);
172
+ const pinoPretty = require('pino-pretty');
173
+ const prettyStream = pinoPretty({
174
+ colorize: true,
175
+ translateTime: 'HH:MM:ss.l',
176
+ ignore: 'pid,hostname',
177
+ singleLine: false,
178
+ });
179
+ return (0, pino_1.default)(pinoOptions, prettyStream);
180
180
  }
181
181
  catch {
182
182
  return (0, pino_1.default)(pinoOptions, process.stdout);
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAa,EAAe,KAAK,MAAM,EAAsB,MAAM,MAAM,CAAC;AAG1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAyBpD,UAAU,oBAAoB;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAwHD,wBAAgB,YAAY,CAAC,GAAG,EAAE,iBAAiB,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAsE/F"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAa,EAAe,KAAK,MAAM,EAAsB,MAAM,MAAM,CAAC;AAG1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AA0CpD,UAAU,oBAAoB;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAwHD,wBAAgB,YAAY,CAAC,GAAG,EAAE,iBAAiB,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CA+D/F"}
package/dist/logger.js CHANGED
@@ -4,20 +4,36 @@ import { ensureWritableTempDirectory } from './env.js';
4
4
  /**
5
5
  * Detects if we're running in a container (deployed environment).
6
6
  * This is used to determine log format: pretty for local dev, JSON for containers.
7
+ *
8
+ * Key indicators we're in a container:
9
+ * - Has CID, PID, HOST, SECRET (Beamable env vars)
10
+ * - NO routing key (deployed services don't use routing keys)
11
+ * - OR explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)
12
+ *
13
+ * Key indicators we're running locally:
14
+ * - Has routing key (local dev always has a routing key)
15
+ * - Running via tsx/node directly (not in Docker)
7
16
  */
8
17
  function isRunningInContainer() {
9
- // Beamable sets CID, PID, HOST, SECRET in deployed containers
10
- // If we have these required env vars but NO routing key, we're in a deployed container
11
- const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
12
- const hasNoRoutingKey = !process.env.NAME_PREFIX && !process.env.ROUTING_KEY;
13
- if (hasBeamableEnvVars && hasNoRoutingKey) {
14
- return true; // Deployed container
15
- }
16
- // Fallback checks for other container indicators
17
- return (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
18
+ // Explicit container indicators (highest priority)
19
+ if (process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
18
20
  process.env.CONTAINER === 'beamable' ||
19
21
  !!process.env.ECS_CONTAINER_METADATA_URI ||
20
- !!process.env.KUBERNETES_SERVICE_HOST);
22
+ !!process.env.KUBERNETES_SERVICE_HOST) {
23
+ return true;
24
+ }
25
+ // If we have a routing key, we're definitely running locally (not in a deployed container)
26
+ // Local dev always sets NAME_PREFIX or ROUTING_KEY
27
+ if (process.env.NAME_PREFIX || process.env.ROUTING_KEY) {
28
+ return false; // Local development
29
+ }
30
+ // If we have Beamable env vars but NO routing key, we're in a deployed container
31
+ const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
32
+ if (hasBeamableEnvVars) {
33
+ return true; // Deployed container (has env vars but no routing key)
34
+ }
35
+ // Default: assume local development
36
+ return false;
21
37
  }
22
38
  /**
23
39
  * Maps Pino log levels to Beamable log levels
@@ -166,21 +182,17 @@ export function createLogger(env, options = {}) {
166
182
  // Try to use pino-pretty if available (optional dependency)
167
183
  // If not available, fall back to default Pino JSON output
168
184
  try {
169
- // Check if pino-pretty is available by attempting to require it
170
- require.resolve('pino-pretty');
171
- // If we get here, pino-pretty is available - use it via transport
172
- return pino({
173
- ...pinoOptions,
174
- transport: {
175
- target: 'pino-pretty',
176
- options: {
177
- colorize: true,
178
- translateTime: 'HH:MM:ss.l',
179
- ignore: 'pid,hostname',
180
- singleLine: false,
181
- },
182
- },
183
- }, process.stdout);
185
+ // Check if pino-pretty is available
186
+ const pinoPretty = require('pino-pretty');
187
+ // Create a pretty stream with formatting options
188
+ const prettyStream = pinoPretty({
189
+ colorize: true,
190
+ translateTime: 'HH:MM:ss.l',
191
+ ignore: 'pid,hostname',
192
+ singleLine: false,
193
+ });
194
+ // Use pino with the pretty stream
195
+ return pino(pinoOptions, prettyStream);
184
196
  }
185
197
  catch {
186
198
  // pino-pretty not available, use default Pino output (JSON but readable)
@@ -1 +1 @@
1
- {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAAE,EAAE,WAAW,EAAmC,MAAM,MAAM,CAAC;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,2BAA2B,EAAE,MAAM,UAAU,CAAC;AAGvD;;;GAGG;AACH,SAAS,oBAAoB;IAC3B,8DAA8D;IAC9D,uFAAuF;IACvF,MAAM,kBAAkB,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC5G,MAAM,eAAe,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;IAE7E,IAAI,kBAAkB,IAAI,eAAe,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC,CAAC,qBAAqB;IACpC,CAAC;IAED,iDAAiD;IACjD,OAAO,CACL,OAAO,CAAC,GAAG,CAAC,2BAA2B,KAAK,MAAM;QAClD,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,UAAU;QACpC,CAAC,CA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C,CAAC,CAAC;QACvE,OAAO,IAAI,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;IAC9C,CAAC;SAAM,CAAC;QACN,MAAM,UAAU,GAAG,WAAW,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACtG,OAAO,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACvC,CAAC;AACH,CAAC","sourcesContent":["import pino, { destination, type Logger, type LoggerOptions } from 'pino';\r\nimport { Transform } from 'node:stream';\r\nimport { ensureWritableTempDirectory } from './env.js';\r\nimport type { EnvironmentConfig } from './types.js';\r\n\r\n/**\r\n * Detects if we're running in a container (deployed environment).\r\n * This is used to determine log format: pretty for local dev, JSON for containers.\r\n */\r\nfunction isRunningInContainer(): boolean {\r\n // Beamable sets CID, PID, HOST, SECRET in deployed containers\r\n // If we have these required env vars but NO routing key, we're in a deployed container\r\n const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);\r\n const hasNoRoutingKey = !process.env.NAME_PREFIX && !process.env.ROUTING_KEY;\r\n \r\n if (hasBeamableEnvVars && hasNoRoutingKey) {\r\n return true; // Deployed container\r\n }\r\n \r\n // Fallback checks for other container indicators\r\n return (\r\n process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||\r\n process.env.CONTAINER === 'beamable' ||\r\n !!process.env.ECS_CONTAINER_METADATA_URI ||\r\n !!process.env.KUBERNETES_SERVICE_HOST\r\n );\r\n}\r\n\r\ninterface LoggerFactoryOptions {\r\n name?: string;\r\n destinationPath?: string;\r\n}\r\n\r\n/**\r\n * Maps Pino log levels to Beamable log levels\r\n * Pino levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal\r\n * Beamable levels: Debug, Info, Warning, Error, Fatal\r\n */\r\nfunction mapPinoLevelToBeamableLevel(level: number): string {\r\n switch (level) {\r\n case 10: // trace\r\n return 'Debug';\r\n case 20: // debug\r\n return 'Debug';\r\n case 30: // info\r\n return 'Info';\r\n case 40: // warn\r\n return 'Warning';\r\n case 50: // error\r\n return 'Error';\r\n case 60: // fatal\r\n return 'Fatal';\r\n default:\r\n return 'Info';\r\n }\r\n}\r\n\r\n/**\r\n * Creates a transform stream that converts Pino JSON logs to Beamable's expected format.\r\n * Beamable expects logs with __t (timestamp), __l (level), and __m (message) fields.\r\n * Pino writes JSON strings (one per line) to the stream.\r\n */\r\nfunction createBeamableLogFormatter(): Transform {\r\n return new Transform({\r\n objectMode: false, // Pino writes strings, not objects\r\n transform(chunk: Buffer, _encoding, callback) {\r\n try {\r\n const line = chunk.toString();\r\n // Skip empty lines\r\n if (!line.trim()) {\r\n callback();\r\n return;\r\n }\r\n \r\n // Parse Pino's JSON log line\r\n const pinoLog = JSON.parse(line);\r\n \r\n // Extract timestamp - Pino uses 'time' field (ISO 8601 string or milliseconds)\r\n // Convert to ISO 8601 string for Beamable\r\n let timestamp: string;\r\n if (typeof pinoLog.time === 'string') {\r\n timestamp = pinoLog.time;\r\n } else if (typeof pinoLog.time === 'number') {\r\n timestamp = new Date(pinoLog.time).toISOString();\r\n } else {\r\n timestamp = new Date().toISOString();\r\n }\r\n \r\n // Map Pino level to Beamable level\r\n const level = mapPinoLevelToBeamableLevel(pinoLog.level);\r\n \r\n // Build the message - combine msg with any additional fields\r\n // Pino's 'msg' field contains the log message\r\n const messageParts: string[] = [];\r\n if (pinoLog.msg) {\r\n messageParts.push(pinoLog.msg);\r\n }\r\n \r\n // Include error information if present\r\n if (pinoLog.err) {\r\n const err = pinoLog.err;\r\n const errMsg = err.message || err.msg || 'Error';\r\n const errStack = err.stack ? `\\n${err.stack}` : '';\r\n messageParts.push(`${errMsg}${errStack}`);\r\n }\r\n \r\n // Build the Beamable log format\r\n const beamableLog: Record<string, unknown> = {\r\n __t: timestamp,\r\n __l: level,\r\n __m: messageParts.length > 0 ? messageParts.join(' ') : 'No message',\r\n };\r\n \r\n // Include additional context fields that might be useful\r\n // These are included in the message object but not as top-level fields\r\n const contextFields: Record<string, unknown> = {};\r\n if (pinoLog.cid) contextFields.cid = pinoLog.cid;\r\n if (pinoLog.pid) contextFields.pid = pinoLog.pid;\r\n if (pinoLog.routingKey) contextFields.routingKey = pinoLog.routingKey;\r\n if (pinoLog.service) contextFields.service = pinoLog.service;\r\n if (pinoLog.component) contextFields.component = pinoLog.component;\r\n \r\n // Include any other fields that aren't standard Pino fields\r\n const standardPinoFields = ['level', 'time', 'pid', 'hostname', 'name', 'msg', 'err', 'v', 'cid', 'pid', 'routingKey', 'sdkVersionExecution', 'service', 'component'];\r\n for (const [key, value] of Object.entries(pinoLog)) {\r\n if (!standardPinoFields.includes(key) && value !== undefined && value !== null) {\r\n contextFields[key] = value;\r\n }\r\n }\r\n \r\n // If there are context fields, include them in the log\r\n if (Object.keys(contextFields).length > 0) {\r\n beamableLog.__c = contextFields;\r\n }\r\n \r\n // Output as a single-line JSON string (required for CloudWatch)\r\n const output = JSON.stringify(beamableLog) + '\\n';\r\n callback(null, Buffer.from(output, 'utf8'));\r\n } catch (error) {\r\n // If parsing fails, output a fallback log entry\r\n const fallbackLog = {\r\n __t: new Date().toISOString(),\r\n __l: 'Error',\r\n __m: `Failed to parse log entry: ${chunk.toString().substring(0, 200)}`,\r\n };\r\n callback(null, Buffer.from(JSON.stringify(fallbackLog) + '\\n', 'utf8'));\r\n }\r\n },\r\n });\r\n}\r\n\r\nexport function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptions = {}): Logger {\r\n const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;\r\n const inContainer = isRunningInContainer();\r\n\r\n const pinoOptions: LoggerOptions = {\r\n name: options.name ?? 'beamable-node-runtime',\r\n level: env.logLevel,\r\n base: {\r\n cid: env.cid,\r\n pid: env.pid,\r\n routingKey: env.routingKey ?? null,\r\n sdkVersionExecution: env.sdkVersionExecution,\r\n },\r\n redact: {\r\n paths: ['secret', 'refreshToken'],\r\n censor: '***',\r\n },\r\n // Use timestamp in milliseconds (Pino default) for accurate conversion\r\n timestamp: pino.stdTimeFunctions.isoTime,\r\n };\r\n\r\n // For deployed services, always log to stdout so container orchestrator can collect logs\r\n // For local development, log to stdout unless a specific file path is provided\r\n if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {\r\n if (inContainer) {\r\n // In containers: Use Beamable JSON format for CloudWatch collection\r\n const beamableFormatter = createBeamableLogFormatter();\r\n beamableFormatter.pipe(process.stdout);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n // Local development: Use Pino's pretty printing for human-readable logs\r\n // Try to use pino-pretty if available (optional dependency)\r\n // If not available, fall back to default Pino JSON output\r\n try {\r\n // Check if pino-pretty is available by attempting to require it\r\n require.resolve('pino-pretty');\r\n // If we get here, pino-pretty is available - use it via transport\r\n return pino(\r\n {\r\n ...pinoOptions,\r\n transport: {\r\n target: 'pino-pretty',\r\n options: {\r\n colorize: true,\r\n translateTime: 'HH:MM:ss.l',\r\n ignore: 'pid,hostname',\r\n singleLine: false,\r\n },\r\n },\r\n },\r\n process.stdout\r\n );\r\n } catch {\r\n // pino-pretty not available, use default Pino output (JSON but readable)\r\n return pino(pinoOptions, process.stdout);\r\n }\r\n }\r\n }\r\n\r\n // For file logging: Use Beamable format in containers, default Pino format locally\r\n const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;\r\n if (inContainer) {\r\n const beamableFormatter = createBeamableLogFormatter();\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n return pino(pinoOptions, fileStream);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"m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Transform } from 'node:stream';\r\nimport { ensureWritableTempDirectory } from './env.js';\r\nimport type { EnvironmentConfig } from './types.js';\r\n\r\n/**\r\n * Detects if we're running in a container (deployed environment).\r\n * This is used to determine log format: pretty for local dev, JSON for containers.\r\n * \r\n * Key indicators we're in a container:\r\n * - Has CID, PID, HOST, SECRET (Beamable env vars)\r\n * - NO routing key (deployed services don't use routing keys)\r\n * - OR explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)\r\n * \r\n * Key indicators we're running locally:\r\n * - Has routing key (local dev always has a routing key)\r\n * - Running via tsx/node directly (not in Docker)\r\n */\r\nfunction isRunningInContainer(): boolean {\r\n // Explicit container indicators (highest priority)\r\n if (\r\n process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||\r\n process.env.CONTAINER === 'beamable' ||\r\n !!process.env.ECS_CONTAINER_METADATA_URI ||\r\n !!process.env.KUBERNETES_SERVICE_HOST\r\n ) {\r\n return true;\r\n }\r\n \r\n // If we have a routing key, we're definitely running locally (not in a deployed container)\r\n // Local dev always sets NAME_PREFIX or ROUTING_KEY\r\n if (process.env.NAME_PREFIX || process.env.ROUTING_KEY) {\r\n return false; // Local development\r\n }\r\n \r\n // If we have Beamable env vars but NO routing key, we're in a deployed container\r\n const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);\r\n if (hasBeamableEnvVars) {\r\n return true; // Deployed container (has env vars but no routing key)\r\n }\r\n \r\n // Default: assume local development\r\n return false;\r\n}\r\n\r\ninterface LoggerFactoryOptions {\r\n name?: string;\r\n destinationPath?: string;\r\n}\r\n\r\n/**\r\n * Maps Pino log levels to Beamable log levels\r\n * Pino levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal\r\n * Beamable levels: Debug, Info, Warning, Error, Fatal\r\n */\r\nfunction mapPinoLevelToBeamableLevel(level: number): string {\r\n switch (level) {\r\n case 10: // trace\r\n return 'Debug';\r\n case 20: // debug\r\n return 'Debug';\r\n case 30: // info\r\n return 'Info';\r\n case 40: // warn\r\n return 'Warning';\r\n case 50: // error\r\n return 'Error';\r\n case 60: // fatal\r\n return 'Fatal';\r\n default:\r\n return 'Info';\r\n }\r\n}\r\n\r\n/**\r\n * Creates a transform stream that converts Pino JSON logs to Beamable's expected format.\r\n * Beamable expects logs with __t (timestamp), __l (level), and __m (message) fields.\r\n * Pino writes JSON strings (one per line) to the stream.\r\n */\r\nfunction createBeamableLogFormatter(): Transform {\r\n return new Transform({\r\n objectMode: false, // Pino writes strings, not objects\r\n transform(chunk: Buffer, _encoding, callback) {\r\n try {\r\n const line = chunk.toString();\r\n // Skip empty lines\r\n if (!line.trim()) {\r\n callback();\r\n return;\r\n }\r\n \r\n // Parse Pino's JSON log line\r\n const pinoLog = JSON.parse(line);\r\n \r\n // Extract timestamp - Pino uses 'time' field (ISO 8601 string or milliseconds)\r\n // Convert to ISO 8601 string for Beamable\r\n let timestamp: string;\r\n if (typeof pinoLog.time === 'string') {\r\n timestamp = pinoLog.time;\r\n } else if (typeof pinoLog.time === 'number') {\r\n timestamp = new Date(pinoLog.time).toISOString();\r\n } else {\r\n timestamp = new Date().toISOString();\r\n }\r\n \r\n // Map Pino level to Beamable level\r\n const level = mapPinoLevelToBeamableLevel(pinoLog.level);\r\n \r\n // Build the message - combine msg with any additional fields\r\n // Pino's 'msg' field contains the log message\r\n const messageParts: string[] = [];\r\n if (pinoLog.msg) {\r\n messageParts.push(pinoLog.msg);\r\n }\r\n \r\n // Include error information if present\r\n if (pinoLog.err) {\r\n const err = pinoLog.err;\r\n const errMsg = err.message || err.msg || 'Error';\r\n const errStack = err.stack ? `\\n${err.stack}` : '';\r\n messageParts.push(`${errMsg}${errStack}`);\r\n }\r\n \r\n // Build the Beamable log format\r\n const beamableLog: Record<string, unknown> = {\r\n __t: timestamp,\r\n __l: level,\r\n __m: messageParts.length > 0 ? messageParts.join(' ') : 'No message',\r\n };\r\n \r\n // Include additional context fields that might be useful\r\n // These are included in the message object but not as top-level fields\r\n const contextFields: Record<string, unknown> = {};\r\n if (pinoLog.cid) contextFields.cid = pinoLog.cid;\r\n if (pinoLog.pid) contextFields.pid = pinoLog.pid;\r\n if (pinoLog.routingKey) contextFields.routingKey = pinoLog.routingKey;\r\n if (pinoLog.service) contextFields.service = pinoLog.service;\r\n if (pinoLog.component) contextFields.component = pinoLog.component;\r\n \r\n // Include any other fields that aren't standard Pino fields\r\n const standardPinoFields = ['level', 'time', 'pid', 'hostname', 'name', 'msg', 'err', 'v', 'cid', 'pid', 'routingKey', 'sdkVersionExecution', 'service', 'component'];\r\n for (const [key, value] of Object.entries(pinoLog)) {\r\n if (!standardPinoFields.includes(key) && value !== undefined && value !== null) {\r\n contextFields[key] = value;\r\n }\r\n }\r\n \r\n // If there are context fields, include them in the log\r\n if (Object.keys(contextFields).length > 0) {\r\n beamableLog.__c = contextFields;\r\n }\r\n \r\n // Output as a single-line JSON string (required for CloudWatch)\r\n const output = JSON.stringify(beamableLog) + '\\n';\r\n callback(null, Buffer.from(output, 'utf8'));\r\n } catch (error) {\r\n // If parsing fails, output a fallback log entry\r\n const fallbackLog = {\r\n __t: new Date().toISOString(),\r\n __l: 'Error',\r\n __m: `Failed to parse log entry: ${chunk.toString().substring(0, 200)}`,\r\n };\r\n callback(null, Buffer.from(JSON.stringify(fallbackLog) + '\\n', 'utf8'));\r\n }\r\n },\r\n });\r\n}\r\n\r\nexport function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptions = {}): Logger {\r\n const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;\r\n const inContainer = isRunningInContainer();\r\n\r\n const pinoOptions: LoggerOptions = {\r\n name: options.name ?? 'beamable-node-runtime',\r\n level: env.logLevel,\r\n base: {\r\n cid: env.cid,\r\n pid: env.pid,\r\n routingKey: env.routingKey ?? null,\r\n sdkVersionExecution: env.sdkVersionExecution,\r\n },\r\n redact: {\r\n paths: ['secret', 'refreshToken'],\r\n censor: '***',\r\n },\r\n // Use timestamp in milliseconds (Pino default) for accurate conversion\r\n timestamp: pino.stdTimeFunctions.isoTime,\r\n };\r\n\r\n // For deployed services, always log to stdout so container orchestrator can collect logs\r\n // For local development, log to stdout unless a specific file path is provided\r\n if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {\r\n if (inContainer) {\r\n // In containers: Use Beamable JSON format for CloudWatch collection\r\n const beamableFormatter = createBeamableLogFormatter();\r\n beamableFormatter.pipe(process.stdout);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n // Local development: Use Pino's pretty printing for human-readable logs\r\n // Try to use pino-pretty if available (optional dependency)\r\n // If not available, fall back to default Pino JSON output\r\n try {\r\n // Check if pino-pretty is available\r\n const pinoPretty = require('pino-pretty');\r\n // Create a pretty stream with formatting options\r\n const prettyStream = pinoPretty({\r\n colorize: true,\r\n translateTime: 'HH:MM:ss.l',\r\n ignore: 'pid,hostname',\r\n singleLine: false,\r\n });\r\n // Use pino with the pretty stream\r\n return pino(pinoOptions, prettyStream);\r\n } catch {\r\n // pino-pretty not available, use default Pino output (JSON but readable)\r\n return pino(pinoOptions, process.stdout);\r\n }\r\n }\r\n }\r\n\r\n // For file logging: Use Beamable format in containers, default Pino format locally\r\n const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;\r\n if (inContainer) {\r\n const beamableFormatter = createBeamableLogFormatter();\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);\r\n return pino(pinoOptions, beamableFormatter);\r\n } else {\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n return pino(pinoOptions, fileStream);\r\n }\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omen.foundation/node-microservice-runtime",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Beamable microservice runtime for Node.js/TypeScript services.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/logger.ts CHANGED
@@ -6,24 +6,41 @@ import type { EnvironmentConfig } from './types.js';
6
6
  /**
7
7
  * Detects if we're running in a container (deployed environment).
8
8
  * This is used to determine log format: pretty for local dev, JSON for containers.
9
+ *
10
+ * Key indicators we're in a container:
11
+ * - Has CID, PID, HOST, SECRET (Beamable env vars)
12
+ * - NO routing key (deployed services don't use routing keys)
13
+ * - OR explicit container indicators (DOTNET_RUNNING_IN_CONTAINER, ECS, K8s)
14
+ *
15
+ * Key indicators we're running locally:
16
+ * - Has routing key (local dev always has a routing key)
17
+ * - Running via tsx/node directly (not in Docker)
9
18
  */
10
19
  function isRunningInContainer(): boolean {
11
- // Beamable sets CID, PID, HOST, SECRET in deployed containers
12
- // If we have these required env vars but NO routing key, we're in a deployed container
13
- const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
14
- const hasNoRoutingKey = !process.env.NAME_PREFIX && !process.env.ROUTING_KEY;
15
-
16
- if (hasBeamableEnvVars && hasNoRoutingKey) {
17
- return true; // Deployed container
18
- }
19
-
20
- // Fallback checks for other container indicators
21
- return (
20
+ // Explicit container indicators (highest priority)
21
+ if (
22
22
  process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
23
23
  process.env.CONTAINER === 'beamable' ||
24
24
  !!process.env.ECS_CONTAINER_METADATA_URI ||
25
25
  !!process.env.KUBERNETES_SERVICE_HOST
26
- );
26
+ ) {
27
+ return true;
28
+ }
29
+
30
+ // If we have a routing key, we're definitely running locally (not in a deployed container)
31
+ // Local dev always sets NAME_PREFIX or ROUTING_KEY
32
+ if (process.env.NAME_PREFIX || process.env.ROUTING_KEY) {
33
+ return false; // Local development
34
+ }
35
+
36
+ // If we have Beamable env vars but NO routing key, we're in a deployed container
37
+ const hasBeamableEnvVars = !!(process.env.CID && process.env.PID && process.env.HOST && process.env.SECRET);
38
+ if (hasBeamableEnvVars) {
39
+ return true; // Deployed container (has env vars but no routing key)
40
+ }
41
+
42
+ // Default: assume local development
43
+ return false;
27
44
  }
28
45
 
29
46
  interface LoggerFactoryOptions {
@@ -183,24 +200,17 @@ export function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptio
183
200
  // Try to use pino-pretty if available (optional dependency)
184
201
  // If not available, fall back to default Pino JSON output
185
202
  try {
186
- // Check if pino-pretty is available by attempting to require it
187
- require.resolve('pino-pretty');
188
- // If we get here, pino-pretty is available - use it via transport
189
- return pino(
190
- {
191
- ...pinoOptions,
192
- transport: {
193
- target: 'pino-pretty',
194
- options: {
195
- colorize: true,
196
- translateTime: 'HH:MM:ss.l',
197
- ignore: 'pid,hostname',
198
- singleLine: false,
199
- },
200
- },
201
- },
202
- process.stdout
203
- );
203
+ // Check if pino-pretty is available
204
+ const pinoPretty = require('pino-pretty');
205
+ // Create a pretty stream with formatting options
206
+ const prettyStream = pinoPretty({
207
+ colorize: true,
208
+ translateTime: 'HH:MM:ss.l',
209
+ ignore: 'pid,hostname',
210
+ singleLine: false,
211
+ });
212
+ // Use pino with the pretty stream
213
+ return pino(pinoOptions, prettyStream);
204
214
  } catch {
205
215
  // pino-pretty not available, use default Pino output (JSON but readable)
206
216
  return pino(pinoOptions, process.stdout);