Skip to content

Commit

Permalink
New: Updated logger settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tgxn committed Jul 9, 2021
1 parent 254930a commit 69d5100
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 57 deletions.
24 changes: 19 additions & 5 deletions bin/orgMedia.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env node

const logger = require("../lib/logger");
const { addFileTransport } = require("../lib/logger");
const { addConsoleTransport, addFileTransport } = require("../lib/logger");

const Organize = require("../lib/organize");

const yargs = require("yargs");
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");

yargs
yargs(hideBin(process.argv))
.option("c", {
alias: "config",
default: "~/.orgMedia/config.json",
Expand All @@ -23,7 +24,13 @@ yargs
.option("l", {
alias: "log",
default: false,
describe: "Log file location"
describe: "log directory path"
})
.option("q", {
alias: "quiet",
type: "boolean",
default: false,
describe: "hide console log output"
})
.command({
command: "$0",
Expand All @@ -43,6 +50,12 @@ function getConfigFromArgv(argv) {
config: argv.config,
storage: argv.storage
};

addConsoleTransport({
level: argv.quiet ? "error" : "debug"
// silent: argv.quiet
});

if (argv.log) {
addFileTransport({
filename: "./logs/organize-%DATE%.log",
Expand All @@ -51,7 +64,8 @@ function getConfigFromArgv(argv) {
maxFiles: 10
});
}
logger.info("Data file paths:", configLocations);

logger.info("data file paths", configLocations);
return configLocations;
}

Expand Down
4 changes: 2 additions & 2 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const logger = require("../lib/logger");
const { createTransport } = require("../lib/logger");
const { addConsoleTransport } = require("../lib/logger");

const Organize = require("../lib/organize");

Expand All @@ -14,4 +13,5 @@ async function run() {
await organizer.organizeAll();
}

addConsoleTransport();
run();
4 changes: 2 additions & 2 deletions bin/watch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const logger = require("../lib/logger");
const { createTransport } = require("../lib/logger");
const { addConsoleTransport } = require("../lib/logger");

const Organize = require("../lib/organize");

Expand All @@ -14,4 +13,5 @@ async function run() {
await organizer.registerWatchers();
}

addConsoleTransport();
run();
74 changes: 38 additions & 36 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,49 @@ const { homedir } = require("os");

const { validate } = require("jsonschema");

const configDataSchema = {
type: "object",
properties: {
enabled: { type: "boolean" },
linkSubtitles: { type: "boolean" },
useHighestQuality: { type: "boolean" },
directories: {
type: "array",
items: [{ type: "string" }]
},
allowedExtensions: {
type: "array",
items: [{ type: "string" }]
},
ignoredExtensions: {
type: "array",
items: [{ type: "string" }]
},
subtitleExtensions: {
type: "array",
items: [{ type: "string" }]
},
allowedSize: {
type: "array",
items: [{ type: "number" }],
minItems: 1,
maxItems: 2
},
targetPath: { type: "string" },
seriesCaseFormat: { type: "string" },
strictType: { type: "string" },
targetFormat: { type: "string" }
},
required: ["directories", "targetPath", "targetFormat"],
additionalProperties: false
};
const logger = require("./logger");

class TaskConfig {
constructor(taskConfigData) {
this.configData = taskConfigData;

const configDataSchema = {
type: "object",
properties: {
enabled: { type: "boolean" },
linkSubtitles: { type: "boolean" },
useHighestQuality: { type: "boolean" },
directories: {
type: "array",
items: [{ type: "string" }]
},
allowedExtensions: {
type: "array",
items: [{ type: "string" }]
},
ignoredExtensions: {
type: "array",
items: [{ type: "string" }]
},
subtitleExtensions: {
type: "array",
items: [{ type: "string" }]
},
allowedSize: {
type: "array",
items: [{ type: "number" }],
minItems: 1,
maxItems: 2
},
targetPath: { type: "string" },
seriesCaseFormat: { type: "string" },
strictType: { type: "string" },
targetFormat: { type: "string" }
},
required: ["directories", "targetPath", "targetFormat"],
additionalProperties: false
};

const validSchema = validate(this.configData, configDataSchema);

if (validSchema.errors.length !== 0) {
Expand Down
26 changes: 14 additions & 12 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
const winston = require("winston");
const { format } = require("winston");
require("winston-daily-rotate-file");

const logger = winston.createLogger({
level: "info",
format: winston.format.simple(),
transports: [
new winston.transports.Console({
// silent: true
})
]
});

function addFileTransport(configOverrides) {
logger.add(
winston.add(
new winston.transports.DailyRotateFile({
filename: "./logs/organize-%DATE%.log",
datePattern: "YYYY-MM-DD-HH",
maxSize: "20m",
maxFiles: 10,
format: format.combine(format.timestamp(), format.simple()),
...configOverrides
})
);
}

function addConsoleTransport(configOverrides) {
winston.add(
new winston.transports.Console({
format: format.combine(format.colorize(), format.cli(), format.simple()),
...configOverrides
})
);
}

module.exports = logger;
module.exports = winston;
module.exports.addFileTransport = addFileTransport;
module.exports.addConsoleTransport = addConsoleTransport;
3 changes: 3 additions & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const path = require("path");

const Queue = require("queue-promise");

const logger = require("./logger");

class Storage {
constructor(filename) {
if (!filename) {
Expand Down Expand Up @@ -31,6 +33,7 @@ class Storage {
data = JSON.parse(fileData);
} catch (e) {
if (e.code !== "ENOENT") {
logger.error("error loading storage", e);
throw e;
}
}
Expand Down

0 comments on commit 69d5100

Please sign in to comment.