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

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
@@ -37,6 +37,17 @@ exports.createLogger = createLogger;
37
37
  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
+ function isRunningInContainer() {
41
+ 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) {
44
+ return true;
45
+ }
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);
50
+ }
40
51
  function mapPinoLevelToBeamableLevel(level) {
41
52
  switch (level) {
42
53
  case 10:
@@ -129,6 +140,7 @@ function createBeamableLogFormatter() {
129
140
  function createLogger(env, options = {}) {
130
141
  var _a, _b, _c;
131
142
  const configuredDestination = (_a = options.destinationPath) !== null && _a !== void 0 ? _a : process.env.LOG_PATH;
143
+ const inContainer = isRunningInContainer();
132
144
  const pinoOptions = {
133
145
  name: (_b = options.name) !== null && _b !== void 0 ? _b : 'beamable-node-runtime',
134
146
  level: env.logLevel,
@@ -145,13 +157,41 @@ function createLogger(env, options = {}) {
145
157
  timestamp: pino_1.default.stdTimeFunctions.isoTime,
146
158
  };
147
159
  if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {
160
+ if (inContainer) {
161
+ const beamableFormatter = createBeamableLogFormatter();
162
+ beamableFormatter.pipe(process.stdout);
163
+ return (0, pino_1.default)(pinoOptions, beamableFormatter);
164
+ }
165
+ else {
166
+ 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);
180
+ }
181
+ catch {
182
+ return (0, pino_1.default)(pinoOptions, process.stdout);
183
+ }
184
+ }
185
+ }
186
+ const resolvedDestination = configuredDestination === 'temp' ? (0, env_js_1.ensureWritableTempDirectory)() : configuredDestination;
187
+ if (inContainer) {
148
188
  const beamableFormatter = createBeamableLogFormatter();
149
- beamableFormatter.pipe(process.stdout);
189
+ const fileStream = (0, pino_1.destination)({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
190
+ beamableFormatter.pipe(fileStream);
150
191
  return (0, pino_1.default)(pinoOptions, beamableFormatter);
151
192
  }
152
- const resolvedDestination = configuredDestination === 'temp' ? (0, env_js_1.ensureWritableTempDirectory)() : configuredDestination;
153
- const beamableFormatter = createBeamableLogFormatter();
154
- const fileStream = (0, pino_1.destination)({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
155
- beamableFormatter.pipe(fileStream);
156
- return (0, pino_1.default)(pinoOptions, beamableFormatter);
193
+ else {
194
+ const fileStream = (0, pino_1.destination)({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
195
+ return (0, pino_1.default)(pinoOptions, fileStream);
196
+ }
157
197
  }
@@ -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;AAEpD,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,CAyC/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;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"}
package/dist/logger.js CHANGED
@@ -1,6 +1,24 @@
1
1
  import pino, { destination } from 'pino';
2
2
  import { Transform } from 'node:stream';
3
3
  import { ensureWritableTempDirectory } from './env.js';
4
+ /**
5
+ * Detects if we're running in a container (deployed environment).
6
+ * This is used to determine log format: pretty for local dev, JSON for containers.
7
+ */
8
+ 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
+ process.env.CONTAINER === 'beamable' ||
19
+ !!process.env.ECS_CONTAINER_METADATA_URI ||
20
+ !!process.env.KUBERNETES_SERVICE_HOST);
21
+ }
4
22
  /**
5
23
  * Maps Pino log levels to Beamable log levels
6
24
  * Pino levels: 10=trace, 20=debug, 30=info, 40=warn, 50=error, 60=fatal
@@ -117,6 +135,7 @@ function createBeamableLogFormatter() {
117
135
  }
118
136
  export function createLogger(env, options = {}) {
119
137
  const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;
138
+ const inContainer = isRunningInContainer();
120
139
  const pinoOptions = {
121
140
  name: options.name ?? 'beamable-node-runtime',
122
141
  level: env.logLevel,
@@ -136,20 +155,50 @@ export function createLogger(env, options = {}) {
136
155
  // For deployed services, always log to stdout so container orchestrator can collect logs
137
156
  // For local development, log to stdout unless a specific file path is provided
138
157
  if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {
139
- // Create a transform stream that formats logs for Beamable
140
- // Pino outputs JSON to the transform, which converts it to Beamable format
158
+ if (inContainer) {
159
+ // In containers: Use Beamable JSON format for CloudWatch collection
160
+ const beamableFormatter = createBeamableLogFormatter();
161
+ beamableFormatter.pipe(process.stdout);
162
+ return pino(pinoOptions, beamableFormatter);
163
+ }
164
+ else {
165
+ // Local development: Use Pino's pretty printing for human-readable logs
166
+ // Try to use pino-pretty if available (optional dependency)
167
+ // If not available, fall back to default Pino JSON output
168
+ 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);
184
+ }
185
+ catch {
186
+ // pino-pretty not available, use default Pino output (JSON but readable)
187
+ return pino(pinoOptions, process.stdout);
188
+ }
189
+ }
190
+ }
191
+ // For file logging: Use Beamable format in containers, default Pino format locally
192
+ const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;
193
+ if (inContainer) {
141
194
  const beamableFormatter = createBeamableLogFormatter();
142
- beamableFormatter.pipe(process.stdout);
143
- // Create Pino logger that writes to the formatter
195
+ const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
196
+ beamableFormatter.pipe(fileStream);
144
197
  return pino(pinoOptions, beamableFormatter);
145
198
  }
146
- // For file logging, also use Beamable format
147
- const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;
148
- const beamableFormatter = createBeamableLogFormatter();
149
- const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
150
- // Pipe the formatted output to the file stream
151
- // Note: destination() returns a SonicBoom stream which is compatible with Node streams
152
- beamableFormatter.pipe(fileStream);
153
- return pino(pinoOptions, beamableFormatter);
199
+ else {
200
+ const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
201
+ return pino(pinoOptions, fileStream);
202
+ }
154
203
  }
155
204
  //# sourceMappingURL=logger.js.map
@@ -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;AAQvD;;;;GAIG;AACH,SAAS,2BAA2B,CAAC,KAAa;IAChD,QAAQ,KAAK,EAAE,CAAC;QACd,KAAK,EAAE,EAAE,QAAQ;YACf,OAAO,OAAO,CAAC;QACjB,KAAK,EAAE,EAAE,QAAQ;YACf,OAAO,OAAO,CAAC;QACjB,KAAK,EAAE,EAAE,OAAO;YACd,OAAO,MAAM,CAAC;QAChB,KAAK,EAAE,EAAE,OAAO;YACd,OAAO,SAAS,CAAC;QACnB,KAAK,EAAE,EAAE,QAAQ;YACf,OAAO,OAAO,CAAC;QACjB,KAAK,EAAE,EAAE,QAAQ;YACf,OAAO,OAAO,CAAC;QACjB;YACE,OAAO,MAAM,CAAC;IAClB,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,SAAS,0BAA0B;IACjC,OAAO,IAAI,SAAS,CAAC;QACnB,UAAU,EAAE,KA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AEvE,OAAO,IAAI,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;AAC9C,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\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\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 // Create a transform stream that formats logs for Beamable\r\n // Pino outputs JSON to the transform, which converts it to Beamable format\r\n const beamableFormatter = createBeamableLogFormatter();\r\n beamableFormatter.pipe(process.stdout);\r\n \r\n // Create Pino logger that writes to the formatter\r\n return pino(pinoOptions, beamableFormatter);\r\n }\r\n\r\n // For file logging, also use Beamable format\r\n const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;\r\n const beamableFormatter = createBeamableLogFormatter();\r\n const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });\r\n // Pipe the formatted output to the file stream\r\n // Note: destination() returns a SonicBoom stream which is compatible with Node streams\r\n beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);\r\n \r\n return pino(pinoOptions, beamableFormatter);\r\n}\r\n"]}
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAA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S,EAAE;wBACT,MAAM,EAAE,aAAa;wBACrB,OAAO,EAAE;4BACP,QAAQ,EAAE,IAAI;4BACd,aAAa,EAAE,YAAY;4BAC3B,MAAM,EAAE,cAAc;4BACtB,UAAU,EAAE,KAAK;yBAClB;qBACF;iBACF,EACD,OAAO,CAAC,MAAM,CACf,CAAC;YACJ,CAAC;YAAC,MAAM,CAAC;gBACP,yEAAyE;gBACzE,OAAO,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IAED,mFAAmF;IACnF,MAAM,mBAAmB,GAAG,qBAAqB,KAAK,MAAM,CAAC,CAAC,CAAC,2BAA2B,EAAE,CAAC,CAAC,CAAC,qBAAqB,CAAC;IACrH,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,iBAAiB,GAAG,0BAA0B,EAAE,CAAC;QACvD,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,iBAAiB,CAAC,IAAI,CAAC,UAA8C,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omen.foundation/node-microservice-runtime",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Beamable microservice runtime for Node.js/TypeScript services.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -33,6 +33,7 @@
33
33
  "keytar": "^7.9.0",
34
34
  "mongodb": "^6.10.0",
35
35
  "pino": "^9.0.0",
36
+ "pino-pretty": "^13.0.0",
36
37
  "reflect-metadata": "^0.2.1",
37
38
  "tar-stream": "^2.2.0",
38
39
  "undici": "^6.19.6",
package/src/logger.ts CHANGED
@@ -3,6 +3,29 @@ import { Transform } from 'node:stream';
3
3
  import { ensureWritableTempDirectory } from './env.js';
4
4
  import type { EnvironmentConfig } from './types.js';
5
5
 
6
+ /**
7
+ * Detects if we're running in a container (deployed environment).
8
+ * This is used to determine log format: pretty for local dev, JSON for containers.
9
+ */
10
+ 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 (
22
+ process.env.DOTNET_RUNNING_IN_CONTAINER === 'true' ||
23
+ process.env.CONTAINER === 'beamable' ||
24
+ !!process.env.ECS_CONTAINER_METADATA_URI ||
25
+ !!process.env.KUBERNETES_SERVICE_HOST
26
+ );
27
+ }
28
+
6
29
  interface LoggerFactoryOptions {
7
30
  name?: string;
8
31
  destinationPath?: string;
@@ -128,6 +151,7 @@ function createBeamableLogFormatter(): Transform {
128
151
 
129
152
  export function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptions = {}): Logger {
130
153
  const configuredDestination = options.destinationPath ?? process.env.LOG_PATH;
154
+ const inContainer = isRunningInContainer();
131
155
 
132
156
  const pinoOptions: LoggerOptions = {
133
157
  name: options.name ?? 'beamable-node-runtime',
@@ -149,22 +173,50 @@ export function createLogger(env: EnvironmentConfig, options: LoggerFactoryOptio
149
173
  // For deployed services, always log to stdout so container orchestrator can collect logs
150
174
  // For local development, log to stdout unless a specific file path is provided
151
175
  if (!configuredDestination || configuredDestination === '-' || configuredDestination === 'stdout' || configuredDestination === 'console') {
152
- // Create a transform stream that formats logs for Beamable
153
- // Pino outputs JSON to the transform, which converts it to Beamable format
154
- const beamableFormatter = createBeamableLogFormatter();
155
- beamableFormatter.pipe(process.stdout);
156
-
157
- // Create Pino logger that writes to the formatter
158
- return pino(pinoOptions, beamableFormatter);
176
+ if (inContainer) {
177
+ // In containers: Use Beamable JSON format for CloudWatch collection
178
+ const beamableFormatter = createBeamableLogFormatter();
179
+ beamableFormatter.pipe(process.stdout);
180
+ return pino(pinoOptions, beamableFormatter);
181
+ } else {
182
+ // Local development: Use Pino's pretty printing for human-readable logs
183
+ // Try to use pino-pretty if available (optional dependency)
184
+ // If not available, fall back to default Pino JSON output
185
+ 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
+ );
204
+ } catch {
205
+ // pino-pretty not available, use default Pino output (JSON but readable)
206
+ return pino(pinoOptions, process.stdout);
207
+ }
208
+ }
159
209
  }
160
210
 
161
- // For file logging, also use Beamable format
211
+ // For file logging: Use Beamable format in containers, default Pino format locally
162
212
  const resolvedDestination = configuredDestination === 'temp' ? ensureWritableTempDirectory() : configuredDestination;
163
- const beamableFormatter = createBeamableLogFormatter();
164
- const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
165
- // Pipe the formatted output to the file stream
166
- // Note: destination() returns a SonicBoom stream which is compatible with Node streams
167
- beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);
168
-
169
- return pino(pinoOptions, beamableFormatter);
213
+ if (inContainer) {
214
+ const beamableFormatter = createBeamableLogFormatter();
215
+ const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
216
+ beamableFormatter.pipe(fileStream as unknown as NodeJS.WritableStream);
217
+ return pino(pinoOptions, beamableFormatter);
218
+ } else {
219
+ const fileStream = destination({ dest: resolvedDestination, mkdir: true, append: true, sync: false });
220
+ return pino(pinoOptions, fileStream);
221
+ }
170
222
  }