Skip to content

Commit bb533b0

Browse files
committed
refactor: improve & standlize the logger #5
Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent a3f3d63 commit bb533b0

File tree

12 files changed

+219
-36
lines changed

12 files changed

+219
-36
lines changed

package-lock.json

Lines changed: 190 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"commander": "^11.1.0",
6161
"i18n": "^0.15.1",
6262
"pino": "^8.17.2",
63+
"pino-pretty": "^11.2.2",
6364
"yaml": "^2.5.1"
6465
},
6566
"devDependencies": {

src/commands/deploy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { deployStack, parseYaml } from '../stack';
2-
import { printer, constructActionContext } from '../common';
2+
import { constructActionContext, logger } from '../common';
33

44
export const deploy = async (
55
stackName: string,
66
options: { location: string; parameters: { [key: string]: string } },
77
) => {
88
const context = constructActionContext(options);
9-
printer.info('Validating yaml...');
9+
logger.info('Validating yaml...');
1010
const iac = parseYaml(context.iacLocation);
11-
printer.success('Yaml is valid! 🎉');
11+
logger.info('Yaml is valid! 🎉');
1212

13-
printer.info('Deploying stack...');
13+
logger.info('Deploying stack...');
1414
await deployStack(stackName, iac, context);
1515

16-
printer.success('Stack deployed! 🎉');
16+
logger.info('Stack deployed! 🎉');
1717
};

src/commands/validate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { printer, constructActionContext } from '../common';
1+
import { constructActionContext, logger } from '../common';
22
import { parseYaml } from '../stack';
33

44
export const validate = (location?: string) => {
55
const context = constructActionContext({ location });
66
parseYaml(context.iacLocation);
7-
printer.success('Yaml is valid! 🎉');
7+
logger.info('Yaml is valid! 🎉');
8+
logger.debug('Yaml is valid! debug🎉');
89
};

src/common/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './printer';
21
export * from './provider';
32
export * from './logger';
43
export * from './getVersion';

src/common/logger.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import pino from 'pino';
22

33
const logger = pino({
44
name: 'ServerlessInsight',
5+
level: ['ServerlessInsight', '*'].includes(process.env.DEBUG || '') ? 'debug' : 'info',
6+
transport: {
7+
target: 'pino-pretty',
8+
},
59
});
610

711
export { logger };

src/common/printer.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/common/rosClient.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import ROS20190910, {
99
} from '@alicloud/ros20190910';
1010
import { Config } from '@alicloud/openapi-client';
1111
import { ActionContext } from '../types';
12-
import { printer } from './printer';
12+
import { logger } from './logger';
1313

1414
const client = new ROS20190910(
1515
new Config({
@@ -98,15 +98,15 @@ export const rosStackDeploy = async (
9898
if (stackInfo) {
9999
const { Status: stackStatus } = stackInfo;
100100
if (stackStatus?.indexOf('IN_PROGRESS') >= 0) {
101-
printer.error(`fail to update stack, because stack status is ${stackStatus}`);
101+
logger.error(`fail to update stack, because stack status is ${stackStatus}`);
102102
throw new Error(`fail to update stack, because stack status is ${stackStatus}`);
103103
}
104104

105-
printer.info(`Update stack: ${stackName} deploying... `);
105+
logger.info(`Update stack: ${stackName} deploying... `);
106106
return await updateStack(stackInfo.stackId as string, templateBody, context);
107107
} else {
108108
// create stack
109-
printer.info(`Create stack: ${stackName} deploying... `);
109+
logger.info(`Create stack: ${stackName} deploying... `);
110110
return await createStack(stackName, templateBody, context);
111111
}
112112
};

src/stack/deploy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as ros from '@alicloud/ros-cdk-core';
22
import { ActionContext, ServerlessIac } from '../types';
3-
import { printer, rosStackDeploy } from '../common';
3+
import { logger, rosStackDeploy } from '../common';
44
import { IacStack } from './iacStack';
55

66
const generateStackTemplate = (stackName: string, iac: ServerlessIac, context: ActionContext) => {
@@ -19,7 +19,7 @@ export const deployStack = async (
1919
context: ActionContext,
2020
) => {
2121
const { template } = generateStackTemplate(stackName, iac, context);
22-
console.log('Generated ROS YAML:', JSON.stringify({ template }));
22+
2323
await rosStackDeploy(stackName, template, context);
24-
printer.info(`Stack deployed! 🎉`);
24+
logger.info(`Stack deployed! 🎉`);
2525
};

src/stack/iacSchema.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RawServerlessIac } from '../types';
22
import Ajv, { ErrorObject } from 'ajv';
3+
import { logger } from '../common';
34

45
const ajv = new Ajv({ allowUnionTypes: true, strict: false, allErrors: true });
56

@@ -130,7 +131,10 @@ class IacSchemaErrors extends Error {
130131
export const validateYaml = (iacJson: RawServerlessIac) => {
131132
const validate = ajv.compile(schema);
132133
const valid = validate(iacJson);
133-
if (!valid) throw new IacSchemaErrors(validate.errors as Array<ErrorObject>);
134+
if (!valid) {
135+
logger.debug(`Invalid yaml: ${JSON.stringify(validate.errors)}`);
136+
throw new IacSchemaErrors(validate.errors as Array<ErrorObject>);
137+
}
134138

135139
return true;
136140
};

0 commit comments

Comments
 (0)