Skip to content

Commit

Permalink
log into a file at /logs
Browse files Browse the repository at this point in the history
  • Loading branch information
patheticGeek committed May 25, 2020
1 parent c3555b3 commit db78ad9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const torrent = require("./routes/torrent");

const dev = process.env.NODE_ENV !== "production";
const PORT = parseInt(process.env.PORT, 10) || 3000;
const useWebserver = process.env.ALLOW_WEB === undefined || process.env.ALLOW_WEB === null;

const server = express();

Expand All @@ -27,11 +28,11 @@ server.use((req, res, next) => {
next();
});

server.get("/ping", (req, res) => {
res.send("pong");
});
server.get("/ping", (req, res) => res.send("pong"));

server.get("/logs", (req, res) => res.sendFile("logs.txt", { root: __dirname }));

server.use("/downloads", express.static("downloads"), serveIndex("downloads", { icons: true }));
useWebserver && server.use("/downloads", express.static("downloads"), serveIndex("downloads", { icons: true }));

server.use("/api/v1/torrent", torrent);
server.use("/api/v1/search", search);
Expand Down
Empty file added logs.txt
Empty file.
31 changes: 18 additions & 13 deletions utils/gdrive.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
const fs = require("fs");
const { google } = require("googleapis");
const logger = require("./logger");
const dev = process.env.NODE_ENV !== "production";
const { CLIENT_ID, CLIENT_SECRET, TOKEN, AUTH_CODE, GDRIVE_PARENT_FOLDER } = dev ? require("../config").creds : process.env;
let parsedToken = null;
if (TOKEN) {
parsedToken = JSON.parse(TOKEN);
try {
parsedToken = JSON.parse(TOKEN);
} catch (e) {
logger("TOKEN env not correct\nTOKEN set to:", TOKEN);
}
}

const SCOPES = ["https://www.googleapis.com/auth/drive.metadata.readonly", "https://www.googleapis.com/auth/drive.file"];

if (!CLIENT_ID) {
console.log("CLIENT_ID env not set. Not uploading to gdrive.");
logger("CLIENT_ID env not set. Not uploading to gdrive.");
}
if (!CLIENT_SECRET) {
console.log("CLIENT_SECRET env not set. Not uploading to gdrive.");
logger("CLIENT_SECRET env not set. Not uploading to gdrive.");
}
if (!AUTH_CODE) {
console.log("AUTH_CODE env not set.");
logger("AUTH_CODE env not set.");
}
if (!TOKEN) {
console.log("TOKEN env not set.");
logger("TOKEN env not set.");
}
if (GDRIVE_PARENT_FOLDER) {
console.log(`GDRIVE_PARENT_FOLDER set to ${GDRIVE_PARENT_FOLDER}`);
logger(`GDRIVE_PARENT_FOLDER set to ${GDRIVE_PARENT_FOLDER}`);
}

let auth = null;
Expand All @@ -33,7 +38,7 @@ if (CLIENT_ID && CLIENT_SECRET) {
if (!a) return;
auth = a;
drive = google.drive({ version: "v3", auth });
console.log("Gdrive client up");
logger("Gdrive client up");
});
}

Expand All @@ -45,7 +50,7 @@ async function authorize() {
access_type: "offline",
scope: SCOPES
});
console.log(`Get AUTH_CODE env by visiting this url: ${authUrl}\n`);
logger(`Get AUTH_CODE env by visiting this url: \n${authUrl}\n`);
return null;
} else if (AUTH_CODE && !TOKEN) {
return oAuth2Client.getToken(AUTH_CODE, (err, token) => {
Expand All @@ -54,16 +59,16 @@ async function authorize() {
return null;
}
oAuth2Client.setCredentials(token);
if (!TOKEN) console.log("Set TOKEN env to: ", JSON.stringify(token));
else console.log("Gdrive config OK.");
if (!TOKEN) logger("Set TOKEN env to: ", JSON.stringify(token));
else logger("Gdrive config OK.");
return oAuth2Client;
});
} else if (AUTH_CODE && TOKEN) {
oAuth2Client.setCredentials(parsedToken);
return oAuth2Client;
} else {
console.log("AUTH_CODE:", !!AUTH_CODE);
console.log("TOKEN:", !!TOKEN);
logger("AUTH_CODE:", !!AUTH_CODE);
logger("TOKEN:", !!TOKEN);
}
}

Expand Down Expand Up @@ -97,7 +102,7 @@ async function uploadFolder(path, parentId) {
const name = intr[intr.length - 1];
if (!fs.existsSync(path)) {
// Check if path exists
console.log(`Path ${path} does not exists`);
logger(`Path ${path} does not exists`);
return null;
}

Expand Down
28 changes: 18 additions & 10 deletions utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ const fs = require("fs");

const logFile = "./logs.txt";

if (!fs.existsSync(logFile)) {
fs.writeFileSync(logFile, "=========== START ==========");
}
fs.writeFileSync(logFile, "");

function logger(data) {
try {
const stringified = JSON.stringify(data);
fs.appendFileSync(logFile, `\n${stringified}`);
} catch (e) {
fs.appendFileSync(logFile, `\n${data}`);
}
function logger(...args) {
let toWrite = "\n";
args.forEach(arg => {
if (typeof arg === "string" || typeof arg === "number") {
toWrite += arg;
} else {
try {
const stringified = JSON.stringify(arg);
toWrite += stringified;
} catch (e) {
toWrite += arg;
}
}
toWrite += " ";
});
console.log(toWrite);
fs.appendFileSync(logFile, toWrite);
}

module.exports = logger;

0 comments on commit db78ad9

Please sign in to comment.