Merged in feature/wrap-logging (pull request #2511)
Feature/wrap logging * feat: change all logging to go through our own logger function so that we can control log levels * move packages/trpc to using our own logger * merge Approved-by: Linus Flood
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
// https://docs.lokalise.com/en/articles/3229161-structured-json
|
||||
|
||||
import { logger } from "@scandic-hotels/common/logger"
|
||||
|
||||
import type { LokaliseMessageDescriptor } from "@/types/intl"
|
||||
|
||||
type TranslationEntry = {
|
||||
@@ -28,7 +30,7 @@ export function format(
|
||||
|
||||
if (description) {
|
||||
if (typeof description === "string") {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
`Unsupported type for description, expected 'object', got ${typeof context}. Skipping!`,
|
||||
msg
|
||||
)
|
||||
@@ -39,7 +41,7 @@ export function format(
|
||||
if (typeof context === "string") {
|
||||
entry.context = context
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
`Unsupported type for context, expected 'string', got ${typeof context}`,
|
||||
msg
|
||||
)
|
||||
@@ -50,7 +52,7 @@ export function format(
|
||||
if (limit && typeof limit === "number") {
|
||||
entry.limit = limit
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
`Unsupported type for limit, expected 'number', got ${typeof limit}`,
|
||||
msg
|
||||
)
|
||||
@@ -64,7 +66,7 @@ export function format(
|
||||
entry.tags = tagArray
|
||||
}
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
`Unsupported type for tags, expected Array, got ${typeof tags}`,
|
||||
msg
|
||||
)
|
||||
@@ -75,7 +77,7 @@ export function format(
|
||||
|
||||
results[id] = entry
|
||||
} else {
|
||||
console.warn(
|
||||
logger.warn(
|
||||
`Skipping message, unsupported type for defaultMessage, expected string, got ${typeof defaultMessage}`,
|
||||
{
|
||||
id,
|
||||
|
||||
@@ -4,16 +4,12 @@ import { performance, PerformanceObserver } from "node:perf_hooks"
|
||||
import { LokaliseApi } from "@lokalise/node-api"
|
||||
import AdmZip from "adm-zip"
|
||||
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
|
||||
const projectId = "4194150766ff28c418f010.39532200"
|
||||
const lokaliseApi = new LokaliseApi({ apiKey: process.env.LOKALISE_API_KEY })
|
||||
|
||||
function log(msg: string, ...args: any[]) {
|
||||
console.log(`[lokalise] ${msg}`, ...args)
|
||||
}
|
||||
|
||||
function error(msg: string, ...args: any[]) {
|
||||
console.error(`[lokalise] ${msg}`, ...args)
|
||||
}
|
||||
const lokaliseLogger = createLogger("lokalise")
|
||||
|
||||
let resolvePerf: (value?: unknown) => void
|
||||
const performanceMetrics = new Promise((resolve) => {
|
||||
@@ -31,7 +27,9 @@ const perf = new PerformanceObserver((items) => {
|
||||
resolvePerf()
|
||||
}
|
||||
} else {
|
||||
log(`[metrics] ${entry.name} completed in ${entry.duration} ms`)
|
||||
lokaliseLogger.info(
|
||||
`[metrics] ${entry.name} completed in ${entry.duration} ms`
|
||||
)
|
||||
}
|
||||
}
|
||||
performance.clearMeasures()
|
||||
@@ -43,7 +41,7 @@ async function waitUntilUploadDone(processId: string) {
|
||||
try {
|
||||
performance.mark("waitUntilUploadDoneStart")
|
||||
|
||||
log("Checking upload status...")
|
||||
lokaliseLogger.debug("Checking upload status...")
|
||||
|
||||
performance.mark("getProcessStart")
|
||||
const process = await lokaliseApi.queuedProcesses().get(processId, {
|
||||
@@ -56,7 +54,7 @@ async function waitUntilUploadDone(processId: string) {
|
||||
"getProcessEnd"
|
||||
)
|
||||
|
||||
log(`Status: ${process.status}`)
|
||||
lokaliseLogger.debug(`Status: ${process.status}`)
|
||||
|
||||
if (process.status === "finished") {
|
||||
clearInterval(interval)
|
||||
@@ -72,8 +70,8 @@ async function waitUntilUploadDone(processId: string) {
|
||||
}
|
||||
} catch (e) {
|
||||
clearInterval(interval)
|
||||
error("An error occurred:", e)
|
||||
performance.mark("waitUntilUploadDoneEnd", { detail: error })
|
||||
lokaliseLogger.error("An error occurred:", e)
|
||||
performance.mark("waitUntilUploadDoneEnd", { detail: e })
|
||||
performance.measure(
|
||||
"Wait on upload",
|
||||
"waitUntilUploadDoneStart",
|
||||
@@ -89,7 +87,7 @@ export async function upload(filepath: string) {
|
||||
perf.observe({ type: "measure" })
|
||||
|
||||
try {
|
||||
log(`Uploading ${filepath}...`)
|
||||
lokaliseLogger.debug(`Uploading ${filepath}...`)
|
||||
|
||||
performance.mark("uploadStart")
|
||||
|
||||
@@ -130,9 +128,9 @@ export async function upload(filepath: string) {
|
||||
"lokaliseUploadEnd"
|
||||
)
|
||||
|
||||
log("Upload successful")
|
||||
lokaliseLogger.debug("Upload successful")
|
||||
} catch (e) {
|
||||
error("Upload failed", e)
|
||||
lokaliseLogger.error("Upload failed", e)
|
||||
} finally {
|
||||
performance.mark("uploadEnd")
|
||||
|
||||
@@ -148,7 +146,7 @@ export async function download(extractPath: string, all: boolean = false) {
|
||||
perf.observe({ type: "measure" })
|
||||
|
||||
try {
|
||||
log(
|
||||
lokaliseLogger.debug(
|
||||
all
|
||||
? "Downloading all translations..."
|
||||
: "Downloading filtered translations..."
|
||||
@@ -199,12 +197,12 @@ export async function download(extractPath: string, all: boolean = false) {
|
||||
"unpackTranslationsEnd"
|
||||
)
|
||||
|
||||
log("Download successful")
|
||||
lokaliseLogger.debug("Download successful")
|
||||
} else {
|
||||
throw bundleResponse
|
||||
}
|
||||
} catch (e) {
|
||||
error("Download failed", e)
|
||||
lokaliseLogger.error("Download failed", e)
|
||||
} finally {
|
||||
performance.mark("downloadEnd")
|
||||
|
||||
@@ -251,11 +249,13 @@ export async function deleteBulk(keyNames: string[]) {
|
||||
.keys()
|
||||
.bulk_delete(keysToDelete, { project_id: projectId })
|
||||
|
||||
log(`Bulk delete successful, removed ${keysToDelete.length} keys`)
|
||||
lokaliseLogger.debug(
|
||||
`Bulk delete successful, removed ${keysToDelete.length} keys`
|
||||
)
|
||||
|
||||
return response
|
||||
} catch (e) {
|
||||
error("Bulk delete failed", e)
|
||||
lokaliseLogger.error("Bulk delete failed", e)
|
||||
} finally {
|
||||
performance.mark("bulkDeleteEnd")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user