Last active
July 29, 2025 13:29
-
-
Save rumart/93ca8e06ded57091570b40a59b665f2f to your computer and use it in GitHub Desktop.
Otel-logger.js example used in blog post https://rudimartinsen.com/2025/07/27/otel-instrumenting-app/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // otel-logger.js | |
| const { logs } = require('@opentelemetry/api-logs'); | |
| // Get the global logger provider (set in instrumentation.js) | |
| const appName = process.env.OTEL_SERVICE_NAME || 'my-app'; | |
| const logger = logs.getLogger(appName); | |
| // Optional: wrap it with convenience methods | |
| function logInfo(message, attributes = {}) { | |
| logger.emit({ | |
| severityText: 'INFO', | |
| body: message, | |
| attributes, | |
| }); | |
| } | |
| function logError(message, attributes = {}) { | |
| logger.emit({ | |
| severityText: 'ERROR', | |
| body: message, | |
| attributes, | |
| }); | |
| } | |
| // Export the logger and helper methods | |
| module.exports = { | |
| logger, | |
| logInfo, | |
| logError, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment