Skip to content
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

chore(medusa): Prefer logger instead of console.log #5846

Merged
merged 3 commits into from
Dec 13, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/nasty-comics-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

chore(medusa): Prefer logger instead of console.log
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") || ""

Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") || ""

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") || ""

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/medusa/src/scripts/create-default-rule-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
})
8 changes: 6 additions & 2 deletions packages/medusa/src/scripts/discount-rule-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
7 changes: 4 additions & 3 deletions packages/medusa/src/scripts/migrate-inventory-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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(
{},
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/scripts/sales-channels-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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}`)
}
}
8 changes: 6 additions & 2 deletions packages/medusa/src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -106,7 +110,7 @@ class AuthService extends TransactionBaseService {
}
}
} catch (error) {
console.log("error ->", error)
this.logger_.log("error ->", error)
// ignore
}

Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
Expand Down