Skip to content

Commit a990fd4

Browse files
authored
Merge pull request #8 from varthe/anime-type
Added logs for metadata
2 parents 250d938 + 7a22a5c commit a990fd4

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

logger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ const winston = require("winston")
33
const DailyRotateFile = require("winston-daily-rotate-file")
44

55
const filePath = process.argv[2] || "./logs"
6+
const logLevel = process.env.LOG_LEVEL || "info"
67

78
const logger = winston.createLogger({
8-
level: process.env.LOG_LEVEL || "info",
9+
level: logLevel,
910
format: winston.format.combine(
1011
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
1112
winston.format.printf(({ timestamp, level, message }) => {

main.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { loadConfig } = require("./configBuilder")
55

66
// Load configuration
77
const config = process.env.NODE_ENV !== "test" ? loadConfig() : ""
8+
const debug = logger.isLevelEnabled("debug")
89
const app = express()
910
app.use(express.json())
1011

@@ -27,7 +28,7 @@ const isObject = (value) => typeof value === "object" && value !== null
2728

2829
const isObjectArray = (value) => Array.isArray(value) && value.some((item) => isObject(item))
2930

30-
const formatLogEntry = (entry) => {
31+
const formatDebugLogEntry = (entry) => {
3132
if (Array.isArray(entry)) {
3233
return entry
3334
.map((item) => (isObject(item) && item.name ? item.name : isObject(item) ? JSON.stringify(item) : item))
@@ -44,9 +45,9 @@ const formatLogEntry = (entry) => {
4445
return entry
4546
}
4647

47-
const buildLogMessage = (message, details = {}) => {
48+
const buildDebugLogMessage = (message, details = {}) => {
4849
const formattedDetails = Object.entries(details)
49-
.map(([key, value]) => `${key}: ${formatLogEntry(value)}`)
50+
.map(([key, value]) => `${key}: ${formatDebugLogEntry(value)}`)
5051
.join("\n")
5152

5253
return `${message}\n${formattedDetails}`
@@ -99,9 +100,9 @@ const findMatchingInstances = (webhook, data, filters) => {
99100
return false
100101
}
101102

102-
if (logger.isLevelEnabled("debug")) {
103+
if (debug) {
103104
logger.debug(
104-
buildLogMessage("Filter check:", {
105+
buildDebugLogMessage("Filter check:", {
105106
Field: key,
106107
"Filter value": value,
107108
"Request value": requestValue,
@@ -164,11 +165,8 @@ const sendToInstances = async (instances, requestId, data) => {
164165
postData.serverId = instance.server_id
165166
if (instance.quality_profile_id) postData.profileId = instance.quality_profile_id
166167

167-
logger.debug({
168-
message: "Sending configuration to instance",
169-
instance: item,
170-
postData: postData,
171-
})
168+
if (debug)
169+
logger.debug(buildDebugLogMessage("Sending configuration to instance:", { instance: item, postData }))
172170

173171
await applyConfig(requestId, postData)
174172
logger.info(`Configuration applied for request ID ${requestId} on instance "${item}"`)
@@ -203,6 +201,20 @@ app.post("/webhook", async (req, res) => {
203201
logger.info(
204202
`Received request ID ${request.request_id} for ${media.media_type} "${data?.originalTitle || data?.originalName}"`
205203
)
204+
if (debug) {
205+
const cleanMetadata = Object.fromEntries(
206+
Object.entries(data).filter(
207+
([key]) => !["credits", "relatedVideos", "networks", "watchProviders"].includes(key)
208+
)
209+
)
210+
logger.debug(
211+
buildDebugLogMessage("Request details:", {
212+
webhook: JSON.stringify(req.body, null, 2),
213+
metadata: JSON.stringify(cleanMetadata, null, 2),
214+
})
215+
)
216+
}
217+
206218
const instances = findMatchingInstances(req.body, data, config.filters)
207219
const postData = getPostData(req.body)
208220
if (instances) await sendToInstances(instances, request.request_id, postData)
@@ -214,10 +226,10 @@ app.post("/webhook", async (req, res) => {
214226
}
215227
})
216228

217-
// Server initialization
218229
const PORT = process.env.PORT || 8481
219230
const server = app.listen(PORT, () => {
220231
logger.info(`Server is running on port ${PORT}`)
232+
if (debug) logger.debug("Debug logs enabled")
221233
})
222234

223235
module.exports = { findMatchingInstances, server }

0 commit comments

Comments
 (0)