Skip to content

feat(ui): add simple Logger #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/ui/src/design-system/pie-chart/pie-chart.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Tooltip,
} from 'recharts';

import { createLogger } from '../../logger';

import {
PIE_CHART_DEFAULT_COLOR_SET,
PieChartGradientColor,
Expand Down Expand Up @@ -45,6 +47,8 @@ export type PieChartProps<T extends object | { name: string; value: number }> =
? PieChartDefaultKeyProps<T>
: PieChartCustomKeyProps<T>;

const logger = createLogger('PieChart');

const formatPieColor = (color: PieChartColor): string =>
Boolean(PieChartGradientColor[color as PieChartGradientColor])
? `url(#${color})`
Expand Down Expand Up @@ -74,6 +78,19 @@ export const PieChart = <T extends object | { name: string; value: number }>({
}: PieChartProps<T>): JSX.Element => {
const data = inputData.slice(0, colors.length);

if (data.length < inputData.length) {
logger.error({
message: [
'`colors` array length is lower than the `data` array length.',
'The `data` items without corresponding `colors` are not rendered.',
],
data: {
colors,
dataCount: inputData.length,
},
});
}

return (
<ResponsiveContainer aspect={1}>
<RechartsPieChart>
Expand Down
64 changes: 64 additions & 0 deletions packages/ui/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
type LoggableComponent = 'PieChart';
type LogLevel = 'error' | 'warn';

interface LogConfig {
componentName: string;
data?: unknown;
message: string[] | string;
}
type LogFunction = (config: Readonly<Omit<LogConfig, 'componentName'>>) => void;
type Logger = Record<LogLevel, LogFunction>;

const namespace = 'UI-Toolkit';
const namespaceSeparator = '::';
const componentSeparator = ' ';

// https://github.com/microsoft/TypeScript/issues/17002
// eslint-disable-next-line functional/prefer-immutable-types
const formatMessage = (message: string[] | string): string =>
Array.isArray(message) ? `\n${message.join('\n')}` : message;

const buildMessageArguments = ({
componentName,
message,
data,
}: Readonly<LogConfig>): unknown[] =>
[
`${namespace}${namespaceSeparator}${componentName}${componentSeparator}${formatMessage(
message,
)}`,
data,
].filter(Boolean);

const getLogFunctionByLevel = (
logLevel: LogLevel,
componentName: LoggableComponent,
): LogFunction => {
const logFunction = console[logLevel];
return config => {
logFunction(...buildMessageArguments({ ...config, componentName }));
};
};

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = (): void => {};

/**
* Creates scoped logger based on component name.
*
* Please ensure that log messages are clear and concise.
* Include corresponding data either in the `data` property or as `${interpolation}` in the message.
* If message is a `string[]` it will be rendered multiline.
*/
const getLogger = (componentName: LoggableComponent): Logger => ({
warn: getLogFunctionByLevel('warn', componentName),
error: getLogFunctionByLevel('error', componentName),
});

const getMockLogger: typeof getLogger = () => ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding mock loggers, why not just return empty if production?

warn: noop,
error: noop,
});

export const createLogger =
process.env.NODE_ENV === 'development' ? getLogger : getMockLogger;