-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[@astrojs/image] adds a logger to the the image integration #4342
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
718a0fc
WIP: adding a console logger that respect vite.logLevel
ec705dc
adds an optional prefix for messages
ae14d01
remove temporary debug log
8483daf
typo fix
2e45a8a
cleaning up log syntax
0518815
fixing logger whitespace
f2b568d
adding README docs
2f775f7
test: disable integration logging in tests
f39f496
chore: add changeset
bdafc9a
Merge branch 'main' into feat/image-logger
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,5 @@ | ||
--- | ||
'@astrojs/image': patch | ||
--- | ||
|
||
The integration now includes a logger to better track progress in SSG builds. Use the new `logLevel: "debug"` integration option to see detailed logs of every image transformation built in your project. |
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
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,74 @@ | ||
// eslint-disable no-console | ||
import { bold, cyan, dim, green, red, yellow } from 'kleur/colors'; | ||
|
||
const PREFIX = '@astrojs/image'; | ||
|
||
// Hey, locales are pretty complicated! Be careful modifying this logic... | ||
// If we throw at the top-level, international users can't use Astro. | ||
// | ||
// Using `[]` sets the default locale properly from the system! | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#parameters | ||
// | ||
// Here be the dragons we've slain: | ||
// https://github.com/withastro/astro/issues/2625 | ||
// https://github.com/withastro/astro/issues/3309 | ||
const dateTimeFormat = new Intl.DateTimeFormat([], { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
}); | ||
|
||
export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino | ||
|
||
export interface LogMessage { | ||
level: LoggerLevel; | ||
message: string; | ||
prefix?: boolean; | ||
timestamp?: boolean; | ||
} | ||
|
||
export const levels: Record<LoggerLevel, number> = { | ||
debug: 20, | ||
info: 30, | ||
warn: 40, | ||
error: 50, | ||
silent: 90, | ||
}; | ||
|
||
function getPrefix(level: LoggerLevel, timestamp: boolean) { | ||
let prefix = ''; | ||
|
||
if (timestamp) { | ||
prefix += dim(dateTimeFormat.format(new Date()) + ' '); | ||
} | ||
|
||
switch (level) { | ||
case 'debug': | ||
prefix += bold(green(`[${PREFIX}] `)); | ||
break; | ||
case 'info': | ||
prefix += bold(cyan(`[${PREFIX}] `)); | ||
break; | ||
case 'warn': | ||
prefix += bold(yellow(`[${PREFIX}] `)); | ||
break; | ||
case 'error': | ||
prefix += bold(red(`[${PREFIX}] `)); | ||
break; | ||
} | ||
|
||
return prefix; | ||
} | ||
|
||
const log = (_level: LoggerLevel, dest: (message: string) => void) => | ||
({ message, level, prefix = true, timestamp = true }: LogMessage) => { | ||
if (levels[_level] >= levels[level]) { | ||
dest(`${prefix ? getPrefix(level, timestamp) : ''}${message}`); | ||
} | ||
} | ||
|
||
export const info = log('info', console.info); | ||
export const debug = log('debug', console.debug); | ||
export const warn = log('warn', console.warn); | ||
export const error = log('error', console.error); | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Thanks for this @tony-sull! Nice QOL improvement.
I like allowing this to be configured at the integration level to allow debugging only images but do you think this should use a default set by the existing logging options?
Running
DEBUG=* astro build
orDEBUG=vite:* astro build
is something we support via Vite, so maybe we should be tapping into that same system?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.
Yeah I had considered that but ended up just mimicking core's logger for now. Ideally we could expose Astro's internal logger to all integrations, there's a bit of work to really get that logger ready for outside use first though