Skip to content

Commit

Permalink
Get rid of winston dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Dec 11, 2024
1 parent fff9367 commit 0dbacff
Show file tree
Hide file tree
Showing 36 changed files with 258 additions and 447 deletions.
307 changes: 63 additions & 244 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions packages/api/src/locale-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { log, LogLevel } from '@unchainedshop/logger';
import {
resolveBestCountry,
resolveBestSupported,
Expand All @@ -8,6 +7,9 @@ import {
import { UnchainedCore } from '@unchainedshop/core';
import memoizee from 'memoizee';
import { UnchainedHTTPServerContext } from './context.js';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:api');

export interface UnchainedLocaleContext {
countryContext: string;
Expand Down Expand Up @@ -52,9 +54,9 @@ export const resolveDefaultContext = memoizee(
const countryObject = countries.find((country) => country.isoCode === countryContext);
const currencyContext = resolveBestCurrency(countryObject.defaultCurrencyCode, currencies);

log(`Locale Context: Resolved ${localeContext.baseName} ${countryContext} ${currencyContext}`, {
level: LogLevel.Debug,
});
logger.debug(
`Locale Context: Resolved ${localeContext.baseName} ${countryContext} ${currencyContext}`,
);

const newContext: UnchainedLocaleContext = {
localeContext,
Expand Down
6 changes: 4 additions & 2 deletions packages/api/src/resolvers/mutations/orders/checkoutCart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Context } from '../../../context.js';
import { log, LogLevel } from '@unchainedshop/logger';
import { OrderCheckoutError } from '../../../errors.js';
import { getOrderCart } from '../utils/getOrderCart.js';
import { createLogger, log } from '@unchainedshop/logger';

const logger = createLogger('unchained:api');

export default async function checkoutCart(
root: never,
Expand All @@ -24,7 +26,7 @@ export default async function checkoutCart(
order = await services.orders.checkoutOrder(order._id, transactionContext, context);
return order;
} catch (error) {
log(error.message, { userId, orderId: order._id, level: LogLevel.Error });
logger.error(error.message, { userId, orderId: order._id });
throw new OrderCheckoutError({
userId,
orderId: order._id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Tree, SortOption, SortDirection } from '@unchainedshop/utils';
import { emit, registerEvents } from '@unchainedshop/events';
import { log, LogLevel } from '@unchainedshop/logger';
import {
generateDbFilterById,
findPreservingIds,
Expand Down Expand Up @@ -40,6 +39,9 @@ import {
AssortmentQuery,
InvalidateCacheFn,
} from '../types.js';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:core');

export type AssortmentsModule = {
// Queries
Expand Down Expand Up @@ -308,9 +310,7 @@ export const configureAssortmentsModule = async ({
};

const invalidateCache: AssortmentsModule['invalidateCache'] = async (selector, options) => {
log('Invalidating productId cache for assortments', {
level: LogLevel.Verbose,
});
logger.debug('Invalidating productId cache for assortments');

const assortments = await Assortments.find(
buildFindSelector({ includeInactive: true, includeLeaves: true, ...selector }),
Expand All @@ -323,11 +323,8 @@ export const configureAssortmentsModule = async ({
return total + invalidatedAssortments;
}, Promise.resolve(0));

log(
logger.debug(
`Invalidated productId cache for ${totalInvalidatedAssortments} of ${assortments.length} base assortments`,
{
level: LogLevel.Verbose,
},
);
};

Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/directors/BasePricingAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { createLogger } from '@unchainedshop/logger';
import { IPricingSheet } from './BasePricingSheet.js';
import { IBaseAdapter, PricingCalculation } from '@unchainedshop/utils';
import { Discount } from './BasePricingDirector.js';

const logger = createLogger('unchained:core');

type Order = { _id?: string };
type OrderDiscount = { _id?: string };

Expand Down Expand Up @@ -74,7 +76,7 @@ export const BasePricingAdapter = <
};
},

log(message: string, { level = LogLevel.Debug, ...options } = {}) {
return log(message, { level, ...options });
log(message: string, options) {
return logger.debug(message, options);
},
});
6 changes: 4 additions & 2 deletions packages/core/src/directors/BasePricingDirector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { BaseDirector, IBaseDirector, PricingCalculation } from '@unchainedshop/utils';
import { BasePricingAdapterContext, BasePricingContext, IPricingAdapter } from './BasePricingAdapter.js';
import { IPricingSheet } from './BasePricingSheet.js';
import { OrderDiscountDirector } from './OrderDiscountDirector.js';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:core');

export interface Discount<DiscountConfiguration> {
discountId: string;
Expand Down Expand Up @@ -120,7 +122,7 @@ export const BasePricingDirector = <
calculation = resolvedCalculation.concat(nextCalculationResult);
return calculation;
} catch (error) {
log(error, { level: LogLevel.Error });
logger.error(error);
}
return resolvedCalculation;
}, Promise.resolve([]));
Expand Down
13 changes: 6 additions & 7 deletions packages/core/src/directors/DeliveryDirector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { BaseDirector, IBaseDirector } from '@unchainedshop/utils';
import {
DeliveryAdapterActions,
Expand All @@ -9,6 +8,9 @@ import {
import { DeliveryProvider } from '@unchainedshop/core-delivery';
import { OrderDelivery, OrderDeliveryStatus } from '@unchainedshop/core-orders';
import { Modules } from '../modules.js';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:core');

export type IDeliveryDirector = IBaseDirector<IDeliveryAdapter> & {
sendOrderDelivery: (
Expand Down Expand Up @@ -48,8 +50,7 @@ export const DeliveryDirector: IDeliveryDirector = {
const throughput = await adapter.estimatedDeliveryThroughput(warehousingThroughputTime);
return throughput;
} catch (error) {
log('Delivery Director -> Error while estimating delivery throughput', {
level: LogLevel.Warning,
logger.warn('Delivery Director -> Error while estimating delivery throughput', {
...error,
});
return null;
Expand All @@ -60,8 +61,7 @@ export const DeliveryDirector: IDeliveryDirector = {
try {
return adapter.isActive();
} catch (error) {
log('Delivery Director -> Error while checking if is active', {
level: LogLevel.Warning,
logger.warn('Delivery Director -> Error while checking if is active', {
...error,
});
return false;
Expand All @@ -72,8 +72,7 @@ export const DeliveryDirector: IDeliveryDirector = {
try {
return adapter.isAutoReleaseAllowed();
} catch (error) {
log('Delivery Director -> Error while checking if auto release is allowed', {
level: LogLevel.Warning,
logger.warn('Delivery Director -> Error while checking if auto release is allowed', {
...error,
});
return false;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/directors/EnrollmentDirector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { BaseDirector, IBaseDirector } from '@unchainedshop/utils';
import { EnrollmentAdapterActions, EnrollmentContext, IEnrollmentAdapter } from './EnrollmentAdapter.js';
import type { OrderPosition } from '@unchainedshop/core-orders';
import type { Product, ProductPlan } from '@unchainedshop/core-products';
import { Enrollment } from '@unchainedshop/core-enrollments';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:core');

export type IEnrollmentDirector = IBaseDirector<IEnrollmentAdapter> & {
transformOrderItemToEnrollment: (
Expand All @@ -24,9 +26,7 @@ const findAppropriateAdapters = (productPlan?: ProductPlan) =>
adapterFilter: (Adapter: IEnrollmentAdapter) => {
const activated = Adapter.isActivatedFor(productPlan);
if (!activated) {
log(`Enrollment Director -> ${Adapter.key} (${Adapter.version}) skipped`, {
level: LogLevel.Warning,
});
logger.warn(`Enrollment Director -> ${Adapter.key} (${Adapter.version}) skipped`);
}
return activated;
},
Expand Down
27 changes: 13 additions & 14 deletions packages/core/src/directors/QuotationDirector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { LogLevel, log } from '@unchainedshop/logger';
import { BaseDirector, IBaseDirector } from '@unchainedshop/utils';
import {
QuotationError,
IQuotationAdapter,
QuotationAdapterActions,
QuotationContext,
} from './QuotationAdapter.js';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:core');

export type IQuotationDirector = IBaseDirector<IQuotationAdapter> & {
actions: (quotationContext: QuotationContext, unchainedAPI) => Promise<QuotationAdapterActions>;
Expand All @@ -20,9 +22,7 @@ const findAppropriateAdapters = (quotationContext: QuotationContext, unchainedAP
adapterFilter: (Adapter: IQuotationAdapter) => {
const activated = Adapter.isActivatedFor(quotationContext, unchainedAPI);
if (!activated) {
log(`Quotation Director -> ${Adapter.key} (${Adapter.version}) skipped`, {
level: LogLevel.Warning,
});
logger.warn(`Quotation Director -> ${Adapter.key} (${Adapter.version}) skipped`);
}
return activated;
},
Expand All @@ -47,8 +47,7 @@ export const QuotationDirector: IQuotationDirector = {
try {
return adapter.configurationError();
} catch (error) {
log('QuotationDirector -> Error while checking for configurationError', {
level: LogLevel.Warning,
logger.warn('QuotationDirector -> Error while checking for configurationError', {
...error,
});
return QuotationError.ADAPTER_NOT_FOUND;
Expand All @@ -60,10 +59,12 @@ export const QuotationDirector: IQuotationDirector = {
const isRequired = await adapter.isManualRequestVerificationRequired();
return isRequired;
} catch (error) {
log('QuotationDirector -> Error while checking if is manual request verification required', {
level: LogLevel.Error,
...error,
});
logger.error(
'QuotationDirector -> Error while checking if is manual request verification required',
{
...error,
},
);
return null;
}
},
Expand All @@ -73,8 +74,7 @@ export const QuotationDirector: IQuotationDirector = {
const isRequired = await adapter.isManualProposalRequired();
return isRequired;
} catch (error) {
log('QuotationDirector -> Error while checking if is manual proposal required', {
level: LogLevel.Error,
logger.error('QuotationDirector -> Error while checking if is manual proposal required', {
...error,
});
return null;
Expand All @@ -94,8 +94,7 @@ export const QuotationDirector: IQuotationDirector = {
});
return itemConfiguration;
} catch (error) {
log('QuotationDirector -> Error while transforming item configuration', {
level: LogLevel.Error,
logger.error('QuotationDirector -> Error while transforming item configuration', {
...error,
});
return null;
Expand Down
18 changes: 10 additions & 8 deletions packages/core/src/directors/WarehousingDirector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IBaseDirector, BaseDirector } from '@unchainedshop/utils';
import { log, LogLevel } from '@unchainedshop/logger';
import { WarehousingProvider, TokenSurrogate } from '@unchainedshop/core-warehousing';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:core');

import {
DeliveryDirector,
Expand Down Expand Up @@ -83,7 +85,7 @@ export const WarehousingDirector: IWarehousingDirector = {
const commissioningTime = await adapter.commissioningTime(quantity);
return Math.max(commissioningTime + productionTime, 0);
} catch (error) {
log(error.message, { level: LogLevel.Error, ...error });
logger.error(error);
return NaN;
}
};
Expand All @@ -102,7 +104,7 @@ export const WarehousingDirector: IWarehousingDirector = {
try {
return adapter.isActive();
} catch (error) {
log(error.message, { level: LogLevel.Error });
logger.error(error);
return false;
}
},
Expand All @@ -115,7 +117,7 @@ export const WarehousingDirector: IWarehousingDirector = {
quantity,
};
} catch (error) {
log(error.message, { level: LogLevel.Error, ...error });
logger.error(error);
return null;
}
},
Expand Down Expand Up @@ -150,7 +152,7 @@ export const WarehousingDirector: IWarehousingDirector = {
earliestDelivery,
};
} catch (error) {
log(error.message, { level: LogLevel.Error, ...error });
logger.error(error);
return {};
}
},
Expand All @@ -161,7 +163,7 @@ export const WarehousingDirector: IWarehousingDirector = {
const tokenMetadata = await adapter.tokenMetadata(chainTokenId, referenceDate);
return tokenMetadata;
} catch (error) {
log(error.message, { level: LogLevel.Error, ...error });
logger.error(error);
return {};
}
},
Expand All @@ -172,7 +174,7 @@ export const WarehousingDirector: IWarehousingDirector = {
const isInvalidateable = await adapter.isInvalidateable(chainTokenId, referenceDate);
return isInvalidateable;
} catch (error) {
log(error.message, { level: LogLevel.Error, ...error });
logger.error(error);
return false;
}
},
Expand All @@ -190,7 +192,7 @@ export const WarehousingDirector: IWarehousingDirector = {
};
});
} catch (error) {
log(error.message, { level: LogLevel.Error, ...error });
logger.error(error);
return [];
}
},
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/directors/WorkerDirector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Work, WorkData, WorkResult } from '@unchainedshop/core-worker';
import { log, LogLevel } from '@unchainedshop/logger';
import { BaseDirector, IBaseDirector } from '@unchainedshop/utils';
import { ScheduleData } from '@breejs/later';
import { IWorkerAdapter } from './WorkerAdapter.js';
import { Modules } from '../modules.js';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:core');

export type WorkScheduleConfiguration = Pick<
WorkData,
Expand Down Expand Up @@ -76,7 +78,7 @@ export const WorkerDirector: IWorkerDirector = {
workItemConfiguration.scheduleId || workItemConfiguration.type,
workItemConfiguration,
);
log(
logger.info(
`WorkerDirector -> Configured ${adapter.type} ${adapter.key}@${adapter.version} (${adapter.label}) for Autorun at ${JSON.stringify(workItemConfiguration.schedule?.schedules)}`,
);
},
Expand All @@ -96,16 +98,16 @@ export const WorkerDirector: IWorkerDirector = {
const adapter = WorkerDirector.getAdapterByType(type);

if (!adapter) {
log(`WorkerDirector: No registered adapter for type: ${type}`);
logger.info(`WorkerDirector: No registered adapter for type: ${type}`);
}

try {
const output = await adapter.doWork(input, unchainedAPI, workId);
return output;
} catch (error) {
// DO not use this as flow control. The adapter should catch expected errors and return status: FAILED
log('DO not use this as flow control.', { level: LogLevel.Verbose });
log(`WorkerDirector -> Error doing work ${type}: ${error.message}`);
logger.debug('DO not use this as flow control.');
logger.info(`WorkerDirector -> Error doing work ${type}: ${error.message}`);

const errorOutput = { error, success: false };
return errorOutput;
Expand Down
Loading

0 comments on commit 0dbacff

Please sign in to comment.