Skip to content

Commit 242f444

Browse files
committed
Improve logging
Add timestamps and output stack trace on error messages
1 parent c6e91b1 commit 242f444

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ const LANG_TO_EXTENSION = {
2525

2626
const delay = ms => new Promise(res => setTimeout(res, ms));
2727

28+
function log(message) {
29+
console.log(`[${new Date().toUTCString()}] ${message}`);
30+
}
31+
2832
function normalizeName(problemName) {
2933
return problemName.toLowerCase().replace(/\s/g, '_');
3034
}
3135

3236
async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) {
3337
const name = normalizeName(submission.title);
34-
console.log(`Committing solution for ${name}...`);
38+
log(`Committing solution for ${name}...`);
3539

3640
if (!LANG_TO_EXTENSION[submission.lang]) {
3741
throw `Language ${submission.lang} does not have a registered extension.`;
@@ -79,7 +83,7 @@ async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA,
7983
force: true
8084
});
8185

82-
console.log(`Committed solution for ${name}`);
86+
log(`Committed solution for ${name}`);
8387

8488
return [treeResponse.data.sha, commitResponse.data.sha];
8589
}
@@ -152,18 +156,17 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT
152156
'Cookie': `csrftoken=${leetcodeCSRFToken};LEETCODE_SESSION=${leetcodeSession};`,
153157
},
154158
};
155-
console.log(`Getting submission from LeetCode, offset ${offset}`);
159+
log(`Getting submission from LeetCode, offset ${offset}`);
156160

157161
const getSubmissions = async (maxRetries, retryCount = 0) => {
158162
try {
159163
const response = await axios.get('https://leetcode.com/api/submissions/', config);
160164
return response;
161165
} catch (exception) {
162166
if (retryCount >= maxRetries) {
163-
console.log(exception);
164167
throw exception;
165168
}
166-
console.log('Error fetching submissions, retrying in ' + 3 ** retryCount + ' seconds...');
169+
log('Error fetching submissions, retrying in ' + 3 ** retryCount + ' seconds...');
167170
// There's a rate limit on LeetCode API, so wait with backoff before retrying.
168171
await delay(3 ** retryCount * 1000);
169172
return getSubmissions(maxRetries, retryCount + 1);
@@ -191,17 +194,17 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT
191194
repo: repo,
192195
});
193196
const defaultBranch = repoInfo.data.default_branch;
194-
console.log(`Default branch for ${owner}/${repo}: ${defaultBranch}`);
197+
log(`Default branch for ${owner}/${repo}: ${defaultBranch}`);
195198
// Write in reverse order (oldest first), so that if there's errors, the last sync time
196199
// is still valid.
197-
console.log(`Syncing ${submissions.length} submissions...`);
200+
log(`Syncing ${submissions.length} submissions...`);
198201
let latestCommitSHA = commits.data[0].sha;
199202
let treeSHA = commits.data[0].commit.tree.sha;
200203
for (i = submissions.length - 1; i >= 0; i--) {
201204
submission = submissions[i];
202205
[treeSHA, latestCommitSHA] = await commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission);
203206
}
204-
console.log('Done syncing all submissions.');
207+
log('Done syncing all submissions.');
205208
}
206209

207210
async function main() {
@@ -215,4 +218,8 @@ async function main() {
215218
await sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession);
216219
}
217220

218-
main().catch(error => core.setFailed(error));
221+
main().catch((error) => {
222+
log(error.stack);
223+
core.setFailed(error)
224+
});
225+

0 commit comments

Comments
 (0)