-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add LoggerVercelTransport
and use it in AvaTax app
#1622
Merged
+79
−18
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4020dcc
Add vercel transport for logger
krzysztofzuraw 3a407bf
Move to require
krzysztofzuraw 4f0f643
Last touches
krzysztofzuraw b8cf254
Fix test
krzysztofzuraw 098faaa
Add changesets
krzysztofzuraw dc4f0c7
Merge branch 'main' into shopx-1510
krzysztofzuraw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
"@saleor/apps-logger": patch | ||
--- | ||
|
||
Added new transport `LoggerVercelTransport`. It is currently in experimental stage but it can be used to send logs directly to Vercel log drain. This transport has optional argument of `loggerContext` - if used you need to make sure that function is executed only on the server. | ||
|
||
Usage: | ||
|
||
```ts | ||
import { logger } from "@saleor/apps-logger"; | ||
import { attachLoggerVercelTransport } from "@saleor/apps-logger/node"; | ||
import { loggerContext } from "./logger-context"; | ||
|
||
attachLoggerVercelTransport(logger, loggerContext); | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"app-avatax": patch | ||
--- | ||
|
||
Added new `LoggerVercelTransport` support. It will help us send logs to our infrastructure without need of OTEL unstable logs API. |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import { attachLoggerConsoleTransport, createLogger, logger } from "@saleor/apps-logger"; | ||
|
||
import packageJson from "../package.json"; | ||
|
||
logger.settings.maskValuesOfKeys = ["metadata", "username", "password", "apiKey"]; | ||
|
||
if (process.env.NODE_ENV !== "production") { | ||
attachLoggerConsoleTransport(logger); | ||
} | ||
|
||
if (typeof window === "undefined") { | ||
import("@saleor/apps-logger/node").then( | ||
async ({ attachLoggerOtelTransport, attachLoggerSentryTransport, LoggerContext }) => { | ||
const loggerContext = await import("./logger-context").then((m) => m.loggerContext); | ||
// Don't remove require - it's necessary for proper logger initialization | ||
const { | ||
attachLoggerSentryTransport, | ||
attachLoggerVercelTransport, | ||
} = require("@saleor/apps-logger/node"); | ||
|
||
attachLoggerSentryTransport(logger); | ||
|
||
attachLoggerSentryTransport(logger); | ||
attachLoggerOtelTransport(logger, packageJson.version, loggerContext); | ||
}, | ||
); | ||
if (process.env.NODE_ENV === "production") { | ||
attachLoggerVercelTransport(logger, require("./logger-context").loggerContext); | ||
} | ||
} | ||
|
||
export { createLogger, logger }; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { logger, createLogger } from "./src/logger"; | ||
export { createLogger, logger } from "./src/logger"; | ||
export { attachLoggerConsoleTransport } from "./src/logger-console-transport"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { LoggerContext, wrapWithLoggerContext } from "./src/logger-context"; | ||
export * from "./src/logger-otel-transport"; | ||
export * from "./src/logger-sentry-transport"; | ||
export * from "./src/logger-vercel-transport"; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ILogObj, Logger } from "tslog"; | ||
|
||
import { LoggerContext } from "./logger-context"; | ||
|
||
export const attachLoggerVercelTransport = ( | ||
logger: Logger<ILogObj>, | ||
loggerContext?: LoggerContext, | ||
) => { | ||
logger.attachTransport((log) => { | ||
const { message, attributes, _meta } = log; | ||
|
||
const bodyMessage = log._meta.name ? `[${log._meta.name}] ${message}` : message; | ||
|
||
const stringifiedMessage = JSON.stringify({ | ||
message: bodyMessage, | ||
...attributes, | ||
...loggerContext?.getRawContext(), | ||
_meta: { | ||
..._meta, | ||
// used to filter out log in log drain | ||
source: "saleor-app", | ||
}, | ||
}); | ||
|
||
// Prints Vercel log in proper level https://vercel.com/docs/observability/runtime-logs#level | ||
if (_meta.logLevelName === "ERROR") { | ||
console.error(stringifiedMessage); | ||
return; | ||
} | ||
|
||
if (_meta.logLevelName === "WARN") { | ||
console.warn(stringifiedMessage); | ||
return; | ||
} | ||
|
||
console.log(stringifiedMessage); | ||
}); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting that it works but technically js doesn't preserve keys order (or guarantees it), so it may be flaky - one day stringified object can be different
but non blocking, if it works, we dont have to touch it