Skip to content

Commit

Permalink
Issue32 logs improvement (#44)
Browse files Browse the repository at this point in the history
* logger function created

* logs folder added to store logs

* utility function to show output

* logProcessOutput function to show stdout and stderr

* app.log added in .gitignore

* app.log path corrected

* file app.log deleted
  • Loading branch information
HeeManSu authored Mar 18, 2024
1 parent 85929be commit 3994995
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
*.env
*.env
/logs/app.log
11 changes: 4 additions & 7 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
execPromise,
exists,
installDependencies,
isIAllApps
isIAllApps,
logProcessOutput
} from './utils/utils';

import { PackageError } from '@metacall/protocol/package';
Expand Down Expand Up @@ -236,12 +237,8 @@ export const deploy = catchAsync(
currentFile
});

proc.stdout?.on('data', (data: Buffer) => {
console.log(data.toString().green);
});
proc.stderr?.on('data', (data: Buffer) => {
console.log(data.toString().red);
});
logProcessOutput(proc.stdout, 'green');
logProcessOutput(proc.stderr, 'red');

proc.on('message', (data: childProcessResponse) => {
if (data.type === protocol.g) {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as fs from 'fs';
import * as path from 'path';

const logFilePath = path.join(__dirname, '../../logs/');
const logFileName = 'app.log';
const logFileFullPath = path.join(logFilePath, logFileName);

export const logger = {
log: (message: string): void => {
const timeStamp = new Date().toISOString();
const logMessage = `${timeStamp} - ${message}\n`;
console.log(message);
if (!fs.existsSync(logFileFullPath)) {
fs.writeFileSync(logFileFullPath, '', { encoding: 'utf-8' });
}
fs.appendFileSync(logFileFullPath, logMessage, { encoding: 'utf-8' });
}
};
10 changes: 10 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
IAllApps,
InspectObject
} from '../constants';
import { logger } from './logger';

export const dirName = (gitUrl: string): string =>
String(gitUrl.split('/')[gitUrl.split('/').length - 1]).replace('.git', '');
Expand Down Expand Up @@ -139,3 +140,12 @@ export const diff = (
export function isIAllApps(data: unknown): data is IAllApps {
return typeof data === 'object' && data !== null;
}

export function logProcessOutput(
proc: NodeJS.ReadableStream | null,
color: 'green' | 'red'
): void {
proc?.on('data', (data: Buffer) => {
logger.log(data.toString()[color]);
});
}

0 comments on commit 3994995

Please sign in to comment.