feat(plugin): expose logging API to plugins#29163
Open
jamesmurdza wants to merge 1 commit into
Open
Conversation
Add a PluginLogger type and a per-plugin log object on PluginInput so plugins can write to OpenCode's log system instead of console.log. Each plugin receives its own logger namespaced as plugin.<id>, where <id> is the package name for installed plugins or the file basename for file:// plugins. The logger is created via Log.create and injected when the plugin loads. Update the existing plugin auth-test fixtures to supply the now-required log field. Signed-off-by: James Murdza <james@jamesmurdza.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue for this PR
Closes #27285
Type of change
What does this PR do?
Plugins currently have no way to write to OpenCode's log files — they can only use
console.log, which isn't captured anywhere useful.This adds a
logobject to thePluginInputthat every plugin receives, withdebug,info,warn, anderrormethods. Each plugin gets its own logger namespaced asplugin.<id>, so its output is written to OpenCode's normal log files and is attributable to the specific plugin.How it works:
packages/plugin/src/index.ts— adds the publicPluginLoggertype and alog: PluginLoggerfield onPluginInput.packages/opencode/src/plugin/index.ts— builds a per-plugin logger viaLog.create({ service: "plugin.<id>" })and injects it when each plugin loads. The<id>is the package name for installed plugins, or the file basename forfile://plugins (e.g.…/my-plugin/index.ts→my-plugin,…/my-plugin.ts→my-plugin).Since
logis now a required field onPluginInput, the existing plugin auth-test fixtures are updated to supply it.How did you verify your code works?
Manually, by running the real CLI with a plugin and confirming its log output appears, correctly namespaced. Minimal repro:
Create
myplugin.tsanywhere:In any project, point
opencode.jsonat it (use the absolute path):{ "$schema": "https://opencode.ai/config.json", "plugin": ["file:///absolute/path/to/myplugin.ts"] }Run any command that boots the project, with logs printed to the terminal:
Confirm the output contains the plugin's lines, namespaced
service=plugin.myplugin:This confirms the
logobject is injected, all four levels are written, and theplugin.<basename>namespace is derived correctly. Re-running with--log-level INFOdrops the DEBUG line, confirming level filtering flows through the plugin logger.Screenshots / recordings
Not a UI change.
Checklist