diff --git a/.changeset/nasty-comics-leave.md b/.changeset/nasty-comics-leave.md new file mode 100644 index 0000000000000..d3ce3ed04c6a2 --- /dev/null +++ b/.changeset/nasty-comics-leave.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +chore(medusa): Prefer logger instead of console.log diff --git a/packages/medusa/src/api/routes/admin/orders/request-return.ts b/packages/medusa/src/api/routes/admin/orders/request-return.ts index 8d9c4a2bf50e3..6b040344901c8 100644 --- a/packages/medusa/src/api/routes/admin/orders/request-return.ts +++ b/packages/medusa/src/api/routes/admin/orders/request-return.ts @@ -18,6 +18,7 @@ import { import { FindParams } from "../../../../types/common" import { OrdersReturnItem } from "../../../../types/orders" import { cleanResponseData } from "../../../../utils/clean-response-data" +import { Logger } from "@medusajs/types" /** * @oas [post] /admin/orders/{id}/return @@ -105,6 +106,7 @@ export default async (req, res) => { const idempotencyKeyService = req.scope.resolve("idempotencyKeyService") const manager: EntityManager = req.scope.resolve("manager") + const logger: Logger = req.scope.resolve("logger") const headerKey = req.get("Idempotency-Key") || "" @@ -284,7 +286,7 @@ export default async (req, res) => { res.status(idempotencyKey.response_code).json(idempotencyKey.response_body) } catch (err) { - console.log(err) + logger.log(err) throw err } } diff --git a/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts b/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts index cae4dd8bba2da..d178cb8ff85d2 100644 --- a/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts +++ b/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts @@ -3,6 +3,7 @@ import { CartService, IdempotencyKeyService } from "../../../../services" import { EntityManager } from "typeorm" import { IdempotencyKey } from "../../../../models/idempotency-key" import { cleanResponseData } from "../../../../utils/clean-response-data" +import { Logger } from "@medusajs/types" /** * @oas [post] /store/carts/{id}/taxes @@ -49,6 +50,7 @@ export default async (req, res) => { "idempotencyKeyService" ) const manager: EntityManager = req.scope.resolve("manager") + const logger: Logger = req.scope.resolve("logger") const headerKey = req.get("Idempotency-Key") || "" @@ -61,7 +63,7 @@ export default async (req, res) => { .initializeRequest(headerKey, req.method, req.params, req.path) }) } catch (error) { - console.log(error) + logger.log(error) res.status(409).send("Failed to create idempotency key") return } diff --git a/packages/medusa/src/api/routes/store/carts/complete-cart.ts b/packages/medusa/src/api/routes/store/carts/complete-cart.ts index bfc204d5167bc..c94b4a568ed02 100644 --- a/packages/medusa/src/api/routes/store/carts/complete-cart.ts +++ b/packages/medusa/src/api/routes/store/carts/complete-cart.ts @@ -3,6 +3,7 @@ import { AbstractCartCompletionStrategy } from "../../../../interfaces" import { IdempotencyKey } from "../../../../models" import { IdempotencyKeyService } from "../../../../services" import { cleanResponseData } from "../../../../utils/clean-response-data" +import { Logger } from "@medusajs/types" /** * @oas [post] /store/carts/{id}/complete @@ -66,6 +67,7 @@ export default async (req, res) => { const idempotencyKeyService: IdempotencyKeyService = req.scope.resolve( "idempotencyKeyService" ) + const logger: Logger = req.scope.resolve("logger") const headerKey = req.get("Idempotency-Key") || "" @@ -77,7 +79,7 @@ export default async (req, res) => { .initializeRequest(headerKey, req.method, req.params, req.path) }) } catch (error) { - console.log(error) + logger.log(error) res.status(409).send("Failed to create idempotency key") return } diff --git a/packages/medusa/src/api/routes/store/gift-cards/get-gift-card.ts b/packages/medusa/src/api/routes/store/gift-cards/get-gift-card.ts index 20bdd66a13e2d..14bd377a731ec 100644 --- a/packages/medusa/src/api/routes/store/gift-cards/get-gift-card.ts +++ b/packages/medusa/src/api/routes/store/gift-cards/get-gift-card.ts @@ -1,6 +1,7 @@ import { defaultStoreGiftCardFields, defaultStoreGiftCardRelations } from "." import GiftCardService from "../../../../services/gift-card" +import { Logger } from "@medusajs/types" /** * @oas [get] /store/gift-cards/{code} @@ -58,7 +59,8 @@ export default async (req, res) => { res.json({ gift_card: giftCard }) } catch (error) { - console.log(error) + const logger: Logger = req.scope.resolve("logger") + logger.log(error) throw error } } diff --git a/packages/medusa/src/api/routes/store/returns/create-return.ts b/packages/medusa/src/api/routes/store/returns/create-return.ts index 91069ab1b7f3b..2e36a844edbda 100644 --- a/packages/medusa/src/api/routes/store/returns/create-return.ts +++ b/packages/medusa/src/api/routes/store/returns/create-return.ts @@ -16,6 +16,7 @@ import IdempotencyKeyService from "../../../../services/idempotency-key" import ReturnService from "../../../../services/return" import { validator } from "../../../../utils/validator" import { defaultRelations } from "." +import { Logger } from "@medusajs/types" /** * @oas [post] /store/returns @@ -225,7 +226,8 @@ export default async (req, res) => { res.status(idempotencyKey.response_code).json(idempotencyKey.response_body) } catch (err) { - console.log(err) + const logger: Logger = req.scope.resolve("logger") + logger.log(err) throw err } } diff --git a/packages/medusa/src/scripts/create-default-rule-types.ts b/packages/medusa/src/scripts/create-default-rule-types.ts index b5d2612c36cea..10296687a7113 100644 --- a/packages/medusa/src/scripts/create-default-rule-types.ts +++ b/packages/medusa/src/scripts/create-default-rule-types.ts @@ -2,6 +2,7 @@ import { createDefaultRuleTypes } from "./utils/create-default-rule-types" import dotenv from "dotenv" import express from "express" import loaders from "../loaders" +import Logger from "../loaders/logger" dotenv.config() @@ -18,10 +19,10 @@ const migrate = async function ({ directory }) { migrate({ directory: process.cwd() }) .then(() => { - console.log("Created default rule types") + Logger.log("Created default rule types") process.exit() }) .catch(() => { - console.log("Failed to create rule types") + Logger.log("Failed to create rule types") process.exit(1) }) diff --git a/packages/medusa/src/scripts/discount-rule-migration.ts b/packages/medusa/src/scripts/discount-rule-migration.ts index 291f09548940b..fd87cd8ac6fe0 100644 --- a/packages/medusa/src/scripts/discount-rule-migration.ts +++ b/packages/medusa/src/scripts/discount-rule-migration.ts @@ -2,7 +2,11 @@ import dotenv from "dotenv" import { DataSource } from "typeorm" import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity" import Logger from "../loaders/logger" -import { DiscountCondition, DiscountConditionOperator, DiscountConditionType, } from "../models/discount-condition" +import { + DiscountCondition, + DiscountConditionOperator, + DiscountConditionType, +} from "../models/discount-condition" import { DiscountConditionProduct } from "../models/discount-condition-product" import { DiscountRule } from "../models/discount-rule" import { DiscountConditionRepository } from "../repositories/discount-condition" @@ -123,6 +127,6 @@ migrate({ typeormConfig }) Logger.info("Database migration completed successfully") process.exit() }) - .catch((err) => console.log(err)) + .catch((err) => Logger.log(err)) export default migrate diff --git a/packages/medusa/src/scripts/line-item-adjustment-migration.ts b/packages/medusa/src/scripts/line-item-adjustment-migration.ts index 39be9022624c1..c8b2f73cb8393 100644 --- a/packages/medusa/src/scripts/line-item-adjustment-migration.ts +++ b/packages/medusa/src/scripts/line-item-adjustment-migration.ts @@ -1,13 +1,13 @@ import dotenv from "dotenv" import { createConnection, SelectQueryBuilder } from "typeorm" -import Logger from "../loaders/logger" import { LineItem } from "../models/line-item" import { LineItemAdjustment } from "../models/line-item-adjustment" import { typeormConfig } from "./db-config" +import Logger from "../loaders/logger" dotenv.config() -const migrate = async function({ typeormConfig }) { +const migrate = async function ({ typeormConfig }) { const connection = await createConnection(typeormConfig) const BATCH_SIZE = 1000 @@ -98,6 +98,6 @@ migrate({ typeormConfig }) Logger.info("Database migration completed successfully") process.exit() }) - .catch((err) => console.log(err)) + .catch((err) => Logger.log(err)) export default migrate diff --git a/packages/medusa/src/scripts/migrate-inventory-items.ts b/packages/medusa/src/scripts/migrate-inventory-items.ts index e7d26dbd6ab8c..33574412cc348 100644 --- a/packages/medusa/src/scripts/migrate-inventory-items.ts +++ b/packages/medusa/src/scripts/migrate-inventory-items.ts @@ -11,6 +11,7 @@ import dotenv from "dotenv" import express from "express" import loaders from "../loaders" import { promiseAll } from "@medusajs/utils" +import Logger from "../loaders/logger" dotenv.config() @@ -133,7 +134,7 @@ const migrate = async function ({ directory }) { await processBatch(variants, defaultLocationId, container) let processedCount = variants.length - console.log(`Processed ${processedCount} of ${totalCount}`) + Logger.log(`Processed ${processedCount} of ${totalCount}`) while (processedCount < totalCount) { const nextBatch = await variantService.list( {}, @@ -147,10 +148,10 @@ const migrate = async function ({ directory }) { await processBatch(nextBatch, defaultLocationId, container) processedCount += nextBatch.length - console.log(`Processed ${processedCount} of ${totalCount}`) + Logger.log(`Processed ${processedCount} of ${totalCount}`) } - console.log("Done") + Logger.log("Done") process.exit(0) } diff --git a/packages/medusa/src/scripts/sales-channels-migration.ts b/packages/medusa/src/scripts/sales-channels-migration.ts index a3a48681f3ebe..1c9a7affb0889 100644 --- a/packages/medusa/src/scripts/sales-channels-migration.ts +++ b/packages/medusa/src/scripts/sales-channels-migration.ts @@ -80,6 +80,6 @@ migrate({ typeormConfig }) Logger.info("Database migration completed successfully") process.exit() }) - .catch((err) => console.log(err)) + .catch((err) => Logger.log(err)) export default migrate diff --git a/packages/medusa/src/scripts/utils/migrate-money-amounts-to-pricing-module.ts b/packages/medusa/src/scripts/utils/migrate-money-amounts-to-pricing-module.ts index 4a1e26076acc0..040533d73bb66 100644 --- a/packages/medusa/src/scripts/utils/migrate-money-amounts-to-pricing-module.ts +++ b/packages/medusa/src/scripts/utils/migrate-money-amounts-to-pricing-module.ts @@ -3,6 +3,7 @@ import { MedusaError, promiseAll } from "@medusajs/utils" import { ProductVariantService } from "../../services" import dotenv from "dotenv" +import Logger from "../../loaders/logger" dotenv.config() @@ -78,6 +79,6 @@ export const migrateProductVariantPricing = async function ( await link.create(links) processedCount += variants.length - console.log(`Processed ${processedCount} of ${totalCount}`) + Logger.log(`Processed ${processedCount} of ${totalCount}`) } } diff --git a/packages/medusa/src/services/auth.ts b/packages/medusa/src/services/auth.ts index 14fa4ca587869..e5be124257f49 100644 --- a/packages/medusa/src/services/auth.ts +++ b/packages/medusa/src/services/auth.ts @@ -5,11 +5,13 @@ import { TransactionBaseService } from "../interfaces" import UserService from "./user" import CustomerService from "./customer" import { EntityManager } from "typeorm" +import { Logger } from "@medusajs/types" type InjectedDependencies = { manager: EntityManager userService: UserService customerService: CustomerService + logger: Logger } /** @@ -18,13 +20,15 @@ type InjectedDependencies = { class AuthService extends TransactionBaseService { protected readonly userService_: UserService protected readonly customerService_: CustomerService + protected readonly logger_: Logger - constructor({ userService, customerService }: InjectedDependencies) { + constructor({ userService, customerService, logger }: InjectedDependencies) { // eslint-disable-next-line prefer-rest-params super(arguments[0]) this.userService_ = userService this.customerService_ = customerService + this.logger_ = logger } /** @@ -106,7 +110,7 @@ class AuthService extends TransactionBaseService { } } } catch (error) { - console.log("error ->", error) + this.logger_.log("error ->", error) // ignore } diff --git a/packages/medusa/src/services/notification.ts b/packages/medusa/src/services/notification.ts index ff9096e4bd6c7..e758714b8b27f 100644 --- a/packages/medusa/src/services/notification.ts +++ b/packages/medusa/src/services/notification.ts @@ -198,7 +198,7 @@ class NotificationService extends TransactionBaseService { return promiseAll( subs.map(async (providerId) => { return this.send(eventName, data, providerId).catch((err) => { - console.log(err) + this.logger_.log(err) this.logger_.warn( `An error occured while ${providerId} was processing a notification for ${eventName}: ${err.message}` )