Skip to content

Commit

Permalink
New: Added basic logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tgxn committed Jun 30, 2021
1 parent c195e08 commit 967efb8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
9 changes: 8 additions & 1 deletion bin/orgMedia.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

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

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

const yargs = require("yargs");
Expand All @@ -17,6 +19,11 @@ yargs
describe: "Storage file location",
type: "string"
})
.option("l", {
alias: "log",
default: false,
describe: "Log file location"
})
.command({
command: "$0",
aliases: ["run"],
Expand All @@ -35,7 +42,7 @@ function getConfigFromArgv(argv) {
config: argv.config,
storage: argv.storage
};
console.log("Data file paths:", configLocations);
logger.log("Data file paths:", configLocations);
return configLocations;
}

Expand Down
15 changes: 15 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const winston = require("winston");
require("winston-daily-rotate-file");

const transport = new winston.transports.DailyRotateFile({
filename: "./logs/organize-%DATE%.log",
datePattern: "YYYY-MM-DD-HH",
maxSize: "20m",
maxFiles: 10
});

const logger = winston.createLogger({
transports: [transport]
});

module.exports = logger;
21 changes: 13 additions & 8 deletions lib/organize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const spinnies = new Spinnies({
failColor: "white"
});

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

const Memory = require("./memory");
const Config = require("./config");
// const Checker = require("./checker");

class Organize {
constructor(configLocations) {
Expand All @@ -46,14 +47,18 @@ class Organize {
}

async organizeAll() {
logger.info("run organizeAll()");

for (const index in this.configArray) {
const layer = new OrganizerLayer(this, index);
await layer.organizeDirectory();
}
}

async registerWatchers() {
logger.info("run registerWatchers()");
spinnies.add("watcher", { text: "Starting watchers..." });

for (const index in this.configArray) {
const configData = this.configArray[index];

Expand Down Expand Up @@ -89,23 +94,23 @@ class OrganizerLayer {
}

async onFileCreated(file, stats) {
console.log(`File Created: "${file}".`);
logger.info(`File Created: "${file}".`);

await this.organizeDirectory();
}

async onFileDeleted(file) {
console.log(`File Deleted: "${file}".`);
logger.info(`File Deleted: "${file}".`);

const linkExists = this.memory.findLinkWithSource(file);
if (linkExists) {
console.log("Remove Link:", linkExists.linkPath);
logger.info("Remove Link:", linkExists.linkPath);

try {
await fs.unlink(linkExists.linkPath);
this.memory.deleteLink(linkExists.linkPath);
} catch (error) {
console.log("failed to unlink", linkExists.linkPath, error.toString());
logger.error("failed to unlink", linkExists.linkPath, error.toString());
}
}
}
Expand Down Expand Up @@ -140,7 +145,7 @@ class OrganizerLayer {
existingQualityInteger < newQualityInteger
) {
// more is better?
console.log("Found better episode!", existingQualityInteger, newQualityInteger);
logger.log("Found better episode!", existingQualityInteger, newQualityInteger);
updateSymlink = true;
}
} else if (Number.isInteger(newQualityInteger)) {
Expand All @@ -150,7 +155,7 @@ class OrganizerLayer {
}
}
} else {
// console.log("no old link!");
// logger.log("no old link!");
updateSymlink = true;
}
return updateSymlink;
Expand Down Expand Up @@ -189,7 +194,7 @@ class OrganizerLayer {

if (this.configData.hasOwnProperty("strictType")) {
if (movieOrSeries !== this.configData.strictType) {
console.log("wrong media type, ignoring", movieOrSeries);
logger.log("wrong media type, ignoring", movieOrSeries);
return false;
}
}
Expand Down

0 comments on commit 967efb8

Please sign in to comment.