opencode-logger 0.1.4 → 0.2.1
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/README.md +29 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +2 -2
- package/dist/file-logger.d.ts +7 -2
- package/dist/file-logger.d.ts.map +1 -1
- package/dist/file-logger.js +55 -7
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,4 +24,33 @@ and add:
|
|
|
24
24
|
}
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
## Configuration
|
|
28
|
+
|
|
29
|
+
You can customize the log directory and filename using environment variables.
|
|
30
|
+
|
|
31
|
+
| Variable | Description | Default |
|
|
32
|
+
|----------|-------------|---------|
|
|
33
|
+
| `OPENCODE_LOGGER_DIR` | The directory where logs are stored. Can be absolute or relative to project root. | `logs/opencode` |
|
|
34
|
+
| `OPENCODE_LOGGER_FILENAME` | The filename for the log file. | `log.jsonl` |
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### Setting via CLI
|
|
39
|
+
|
|
40
|
+
You can set them when starting the Opencode client:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
OPENCODE_LOGGER_DIR=/tmp/my-logs opencode
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Setting by exporting ENV variables
|
|
47
|
+
|
|
48
|
+
Or export them, for example in your `~/.bashrc`
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
export OPENCODE_LOGGER_DIR=/tmp/my-logs
|
|
52
|
+
export OPENCODE_LOGGER_FILENAME=custom-name.jsonl
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
|
|
27
56
|
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Directory where logs will be stored relative to the project root.
|
|
3
3
|
*/
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const DEFAULT_LOG_DIRECTORY = "logs/opencode";
|
|
5
5
|
/**
|
|
6
6
|
* Filename for the log file.
|
|
7
7
|
*/
|
|
8
|
-
export declare const
|
|
8
|
+
export declare const DEFAULT_LOG_FILENAME = "log.jsonl";
|
|
9
9
|
/**
|
|
10
10
|
* List of event types supported by the logger plugin.
|
|
11
11
|
* These events correspond to various lifecycle hooks and actions within the Opencode environment.
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,qBAAqB,kBAAkB,CAAC;AAErD;;GAEG;AACH,eAAO,MAAM,oBAAoB,cAAc,CAAC;AAEhD;;;GAGG;AACH,eAAO,MAAM,gBAAgB,yiBA4BnB,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
package/dist/constants.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Directory where logs will be stored relative to the project root.
|
|
3
3
|
*/
|
|
4
|
-
export const
|
|
4
|
+
export const DEFAULT_LOG_DIRECTORY = "logs/opencode";
|
|
5
5
|
/**
|
|
6
6
|
* Filename for the log file.
|
|
7
7
|
*/
|
|
8
|
-
export const
|
|
8
|
+
export const DEFAULT_LOG_FILENAME = "log.jsonl";
|
|
9
9
|
/**
|
|
10
10
|
* List of event types supported by the logger plugin.
|
|
11
11
|
* These events correspond to various lifecycle hooks and actions within the Opencode environment.
|
package/dist/file-logger.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PluginInput } from "@opencode-ai/plugin";
|
|
1
2
|
/**
|
|
2
3
|
* Interface representing a single log entry in the log file.
|
|
3
4
|
*/
|
|
@@ -14,14 +15,18 @@ export interface LogEntry {
|
|
|
14
15
|
*/
|
|
15
16
|
export declare class FileLogger {
|
|
16
17
|
private logFilePath;
|
|
18
|
+
private pluginInput;
|
|
17
19
|
/**
|
|
18
20
|
* Creates a new instance of FileLogger.
|
|
19
21
|
* @param projectRoot - The absolute path to the root of the project.
|
|
22
|
+
* @param pluginInput - Opencode plugin input
|
|
20
23
|
*/
|
|
21
|
-
constructor(projectRoot: string);
|
|
24
|
+
constructor(projectRoot: string, pluginInput: PluginInput);
|
|
25
|
+
private resolveLogDirectory;
|
|
26
|
+
private getLogFilename;
|
|
22
27
|
/**
|
|
23
28
|
* Initializes the logger by ensuring the log directory exists.
|
|
24
|
-
* This method
|
|
29
|
+
* This method must be called before attempting to log any events.
|
|
25
30
|
*/
|
|
26
31
|
init(): Promise<void>;
|
|
27
32
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-logger.d.ts","sourceRoot":"","sources":["../src/file-logger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"file-logger.d.ts","sourceRoot":"","sources":["../src/file-logger.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGvD;;GAEG;AACH,MAAM,WAAW,QAAQ;IACxB,qDAAqD;IACrD,SAAS,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,SAAS,EAAE,MAAM,CAAC;IAClB,kDAAkD;IAClD,OAAO,EAAE,OAAO,CAAC;CACjB;AAED;;GAEG;AACH,qBAAa,UAAU;IACtB,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,WAAW,CAAc;IAEjC;;;;OAIG;gBACS,WAAW,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW;IAQzD,OAAO,CAAC,mBAAmB;IAY3B,OAAO,CAAC,cAAc;IAKtB;;;OAGG;IACG,IAAI;IA8BV;;;;OAIG;IACG,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;CAwB7C"}
|
package/dist/file-logger.js
CHANGED
|
@@ -1,29 +1,68 @@
|
|
|
1
1
|
import { appendFile, mkdir } from "node:fs/promises";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import {
|
|
2
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { DEFAULT_LOG_DIRECTORY, DEFAULT_LOG_FILENAME } from "./constants.js";
|
|
4
4
|
/**
|
|
5
5
|
* Handles initialization of the log directory and writing log entries to the file system.
|
|
6
6
|
*/
|
|
7
7
|
export class FileLogger {
|
|
8
8
|
logFilePath;
|
|
9
|
+
pluginInput;
|
|
9
10
|
/**
|
|
10
11
|
* Creates a new instance of FileLogger.
|
|
11
12
|
* @param projectRoot - The absolute path to the root of the project.
|
|
13
|
+
* @param pluginInput - Opencode plugin input
|
|
12
14
|
*/
|
|
13
|
-
constructor(projectRoot) {
|
|
14
|
-
|
|
15
|
+
constructor(projectRoot, pluginInput) {
|
|
16
|
+
const logDir = this.resolveLogDirectory(projectRoot);
|
|
17
|
+
const logFilename = this.getLogFilename();
|
|
18
|
+
this.logFilePath = join(logDir, logFilename);
|
|
19
|
+
this.pluginInput = pluginInput;
|
|
20
|
+
}
|
|
21
|
+
resolveLogDirectory(projectRoot) {
|
|
22
|
+
const logDir =
|
|
23
|
+
// biome-ignore lint/complexity/useLiteralKeys: process.env access
|
|
24
|
+
process.env["OPENCODE_LOGGER_DIR"] ||
|
|
25
|
+
join(projectRoot, DEFAULT_LOG_DIRECTORY);
|
|
26
|
+
// If logDir is absolute, use it directly. Otherwise, resolve it relative to projectRoot
|
|
27
|
+
// Note: The default join(projectRoot, DEFAULT_LOG_DIRECTORY) above already handles the relative default case,
|
|
28
|
+
// but we check isAbsolute here specifically for the ENV var case if the user passed a relative path.
|
|
29
|
+
return isAbsolute(logDir) ? logDir : resolve(projectRoot, logDir);
|
|
30
|
+
}
|
|
31
|
+
getLogFilename() {
|
|
32
|
+
// biome-ignore lint/complexity/useLiteralKeys: process.env access
|
|
33
|
+
return process.env["OPENCODE_LOGGER_FILENAME"] || DEFAULT_LOG_FILENAME;
|
|
15
34
|
}
|
|
16
35
|
/**
|
|
17
36
|
* Initializes the logger by ensuring the log directory exists.
|
|
18
|
-
* This method
|
|
37
|
+
* This method must be called before attempting to log any events.
|
|
19
38
|
*/
|
|
20
39
|
async init() {
|
|
21
40
|
try {
|
|
22
41
|
const dirPath = join(this.logFilePath, "..");
|
|
23
42
|
await mkdir(dirPath, { recursive: true });
|
|
43
|
+
// NOTE: this opencode log call causes
|
|
44
|
+
// the application to hang on start.
|
|
45
|
+
// Did not see anything in the internal logs,
|
|
46
|
+
// so this is disabled for the moment
|
|
47
|
+
/* await this.pluginInput.client.app.log({
|
|
48
|
+
body: {
|
|
49
|
+
service: "opencode-logger",
|
|
50
|
+
level: "info",
|
|
51
|
+
message: "Plugin initialized!",
|
|
52
|
+
},
|
|
53
|
+
}); */
|
|
24
54
|
}
|
|
25
55
|
catch (error) {
|
|
26
|
-
|
|
56
|
+
await this.pluginInput.client.app.log({
|
|
57
|
+
body: {
|
|
58
|
+
service: "opencode-logger",
|
|
59
|
+
level: "error",
|
|
60
|
+
message: "Failed to create log directory.",
|
|
61
|
+
extra: {
|
|
62
|
+
error,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
});
|
|
27
66
|
}
|
|
28
67
|
}
|
|
29
68
|
/**
|
|
@@ -42,7 +81,16 @@ export class FileLogger {
|
|
|
42
81
|
await appendFile(this.logFilePath, line, "utf-8");
|
|
43
82
|
}
|
|
44
83
|
catch (error) {
|
|
45
|
-
|
|
84
|
+
await this.pluginInput.client.app.log({
|
|
85
|
+
body: {
|
|
86
|
+
service: "opencode-logger",
|
|
87
|
+
level: "error",
|
|
88
|
+
message: "Failed to write to log file.",
|
|
89
|
+
extra: {
|
|
90
|
+
error,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
});
|
|
46
94
|
}
|
|
47
95
|
}
|
|
48
96
|
}
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,7 @@ export const loggerPlugin = async (ctx) => {
|
|
|
11
11
|
// ctx.directory is the project root in the context of the running plugin usually,
|
|
12
12
|
// but let's be safe and check if we need to resolve it.
|
|
13
13
|
// Based on the docs: directory: The current working directory.
|
|
14
|
-
const logger = new FileLogger(ctx.directory);
|
|
14
|
+
const logger = new FileLogger(ctx.directory, ctx);
|
|
15
15
|
await logger.init();
|
|
16
16
|
console.log("[Opencode Logger] Plugin initialized!");
|
|
17
17
|
const hooks = {
|