Skip to content

Commit 493adfe

Browse files
committed
fix logger
1 parent 8286fc1 commit 493adfe

File tree

2 files changed

+53
-41
lines changed

2 files changed

+53
-41
lines changed

src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { Asset } from './asset';
2828
import { Repos } from './repos';
2929
import { Accounts } from './accounts';
3030
import { Search } from './search';
31-
import { logger } from './logger';
31+
import { initLogger, logger } from './logger';
3232

3333
const DEFAULT_PORT = 3000;
3434
const STDIO_OPTION = 'stdio';
@@ -175,7 +175,7 @@ function parseTransportFlag(args: string[]): string {
175175
return transportArg;
176176
}
177177

178-
function parsUsernameFlag(args: string[]): string | undefined {
178+
function parseUsernameFlag(args: string[]): string | undefined {
179179
const usernameArg = args.find((arg) => arg.startsWith('--username='))?.split('=')[1];
180180
if (!usernameArg) {
181181
logger.info('username unspecified');
@@ -185,6 +185,16 @@ function parsUsernameFlag(args: string[]): string | undefined {
185185
return usernameArg;
186186
}
187187

188+
function parseLogsDir(args: string[]): string | undefined {
189+
const logsDirArg = args.find((arg) => arg.startsWith('--logs-dir='))?.split('=')[1];
190+
if (!logsDirArg) {
191+
console.warn('logs dir unspecified');
192+
return undefined;
193+
}
194+
195+
return logsDirArg;
196+
}
197+
188198
function parsePortFlag(args: string[]): number {
189199
const portArg = args.find((arg) => arg.startsWith('--port='))?.split('=')[1];
190200
if (!portArg || portArg.length === 0) {
@@ -204,10 +214,12 @@ function parsePortFlag(args: string[]): number {
204214
// Main execution
205215
async function main() {
206216
const args = process.argv.slice(2);
217+
const logsDir = parseLogsDir(args);
218+
initLogger(logsDir);
207219
logger.info(args.length > 0 ? `provided arguments: ${args}` : 'no arguments provided');
208220
const transportArg = parseTransportFlag(args);
209221
const port = parsePortFlag(args);
210-
const username = parsUsernameFlag(args);
222+
const username = parseUsernameFlag(args);
211223
const patToken = process.env.HUB_PAT_TOKEN;
212224

213225
const server = new HubMCPServer(username, patToken);

src/logger.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,46 @@
1414
limitations under the License.
1515
*/
1616

17+
import path from 'path';
1718
import winston, { format } from 'winston';
19+
export let logger: winston.Logger;
1820

19-
export const logger = winston.createLogger({
20-
level: 'info',
21-
format: format.combine(
22-
format.timestamp({
23-
format: 'YYYY-MM-DD HH:mm:ss',
24-
}),
25-
format.errors({ stack: true }),
26-
format.splat(),
27-
format.json()
28-
),
29-
defaultMeta: { service: 'dockerhub-mcp-server' },
30-
transports: [],
31-
});
21+
export function initLogger(logsDir?: string) {
22+
logger = winston.createLogger({
23+
level: 'info',
24+
format: format.combine(
25+
format.timestamp({
26+
format: 'YYYY-MM-DD HH:mm:ss',
27+
}),
28+
format.errors({ stack: true }),
29+
format.splat(),
30+
format.json()
31+
),
32+
defaultMeta: { service: 'dockerhub-mcp-server' },
33+
transports: [],
34+
});
3235

33-
//
34-
// If we're not in production then log to the `console` with the format:
35-
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
36+
if (process.env.NODE_ENV === 'production' && !logsDir) {
37+
logsDir = '/app/logs';
38+
console.warn(`logs dir unspecified, defaulting to ${logsDir}`);
39+
}
3640

37-
if (process.env.NODE_ENV !== 'production') {
38-
logger.add(
39-
new winston.transports.Console({
40-
format: winston.format.simple(),
41-
log: (info) => {
42-
console.error(info.message);
43-
},
44-
})
45-
);
46-
} else {
47-
logger.transports.push(
48-
//
49-
// - Write all logs with importance level of `error` or higher to `error.log`
50-
// (i.e., error, fatal, but not other levels)
51-
//
52-
new winston.transports.File({ filename: 'logs/error.log', level: 'warn' }),
53-
//
54-
// - Write all logs with importance level of `info` or higher to `combined.log`
55-
// (i.e., fatal, error, warn, and info, but not trace)
56-
//
57-
new winston.transports.File({ filename: 'logs/mcp.log', level: 'info' })
58-
);
41+
if (logsDir) {
42+
logger.transports.push(
43+
new winston.transports.File({
44+
filename: path.join(logsDir, 'error.log'),
45+
level: 'warn',
46+
}),
47+
new winston.transports.File({ filename: path.join(logsDir, 'mcp.log'), level: 'info' })
48+
);
49+
} else {
50+
logger.add(
51+
new winston.transports.Console({
52+
format: winston.format.simple(),
53+
log: (info) => {
54+
console.error(info.message);
55+
},
56+
})
57+
);
58+
}
5959
}

0 commit comments

Comments
 (0)