|
1 | 1 | import service from "@neuralegion/os-service"; |
2 | 2 | import { exec } from "child_process"; |
3 | | -import fs from "fs"; |
4 | 3 | import os from "os"; |
5 | 4 | import path from "path"; |
6 | 5 | import sudo from "@vscode/sudo-prompt"; |
| 6 | +import { createLogger, format, transports } from "winston"; |
7 | 7 |
|
8 | 8 | const isWindows = os.platform() === "win32"; |
9 | 9 | const serviceName = "atest1"; |
@@ -119,15 +119,29 @@ switch (process.argv[2]) { |
119 | 119 | // Need to redirect all output to a log file on Windows. |
120 | 120 | // On Linux, it'll go to the systemd logs. |
121 | 121 | if (isWindows) { |
122 | | - const logStream = fs.createWriteStream( |
123 | | - path.join(__dirname, "service.log"), |
124 | | - { flags: "a" }, |
125 | | - ); |
126 | | - // @ts-expect-error Undefined/null type mismatch: Type 'Error | null | undefined' is not assignable to type 'Error | undefined'. |
127 | | - process.stdout.write = process.stderr.write = |
128 | | - logStream.write.bind(logStream); |
129 | | - process.on("uncaughtException", function (err) { |
130 | | - console.error(err && err.stack ? err.stack : err); |
| 122 | + const logger = createLogger({ |
| 123 | + format: format.combine( |
| 124 | + format.timestamp({ |
| 125 | + format: "YYYY-MM-DD HH:mm:ss", |
| 126 | + }), |
| 127 | + format.printf( |
| 128 | + (info) => `${info.timestamp} ${info.level}: ${info.message}`, |
| 129 | + ), |
| 130 | + ), |
| 131 | + transports: [ |
| 132 | + new transports.File({ |
| 133 | + filename: path.join(__dirname, "service.log"), |
| 134 | + }), |
| 135 | + ], |
| 136 | + }); |
| 137 | + |
| 138 | + console.log = (message: any) => logger.info(message); |
| 139 | + console.info = (message: any) => logger.info(message); |
| 140 | + console.warn = (message: any) => logger.warn(message); |
| 141 | + console.error = (message: any) => logger.error(message); |
| 142 | + |
| 143 | + process.on("uncaughtException", (err) => { |
| 144 | + logger.error(err.stack || err.toString()); |
131 | 145 | }); |
132 | 146 | } |
133 | 147 |
|
|
0 commit comments