Skip to content

Commit

Permalink
🔊 logging updates
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacInsoll committed Sep 23, 2024
1 parent 65921cf commit 95cd2e0
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 19 deletions.
1 change: 1 addition & 0 deletions backend/boot/getVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const getVersion = () => {
try {
picrConfig.version = readFileSync('dist/version.txt', 'utf8');
log(
'info',
'#️⃣ Running version: ' +
(picrConfig.dev ? '[DEV] ' : '') +
picrConfig.version,
Expand Down
2 changes: 1 addition & 1 deletion backend/express/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const expressServer = () => {
});

exp.listen(port, () => {
log(`🌐 App listening at http://localhost:${port}`, true);
log('info', `🌐 App listening at http://localhost:${port}`, true);
});

return exp;
Expand Down
9 changes: 5 additions & 4 deletions backend/filesystem/events/addFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { picrConfig } from '../../server';
export const addFile = async (filePath: string, generateThumbs: boolean) => {
const type = validExtension(filePath);
if (!type) {
log(`🤷‍♂️ Ignoring ${filePath} as it's not a supported file format`);
log('info', `🤷‍♂️ Ignoring ${filePath} as it's not a supported file format`);
return;
}
// console.log(`${basename(filePath)} of type ${type} in ${dirname(filePath)}`);
Expand All @@ -42,6 +42,7 @@ export const addFile = async (filePath: string, generateThumbs: boolean) => {
!created && file.fileLastModified.getTime() != stats.mtime.getTime();
if (created || !file.fileHash || modified) {
log(
'info',
(created
? 'New File: '
: modified
Expand All @@ -66,7 +67,7 @@ export const addFile = async (filePath: string, generateThumbs: boolean) => {
file.imageRatio = meta.Height > 0 ? meta.Width / meta.Height : 0;
}
} else if (picrConfig.updateMetadata) {
log('🔄️ update metadata: ' + file.id);
log('info', '🔄️ update metadata: ' + file.id);
switch (type) {
case 'Image':
file.metadata = JSON.stringify(await getImageMetadata(file));
Expand All @@ -81,15 +82,15 @@ export const addFile = async (filePath: string, generateThumbs: boolean) => {
file.exists = true;
file.save();
// console.log(file);
log('➕ [done]' + filePath);
log('info', '➕ [done]' + filePath);
};

const findFolderId = async (fullPath: string) => {
while (true) {
if (fullPath == directoryPath) return 1;
const id = folderList[relativePath(fullPath)];
if (id && id !== '0') return id;
log('💤 Sleeping waiting for a FolderID for a file in ' + fullPath);
log('info', '💤 Sleeping waiting for a FolderID for a file in ' + fullPath);
await new Promise((r) => setTimeout(r, 500));
}
};
Expand Down
2 changes: 1 addition & 1 deletion backend/filesystem/events/addFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const addFolder = async (path: string) => {
folderList[p] = newFolder.id; // for caching
updateFolderHash(newFolder);
f = newFolder.id;
log(`📁➕ ${relativePath(path)}`);
log('info', `📁➕ ${relativePath(path)}`);
}
// console.log('finished addFolder: ' + path);

Expand Down
5 changes: 4 additions & 1 deletion backend/filesystem/events/removeFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ export const removeFolder = async (path: string) => {
if (newFolder) {
//TODO: Handle folder rename (move data across?)
log(
'info',
`🔀 Appears to be folder Rename from ${folder.relativePath} to ${newFolder.relativePath}`,
);
}
folderList[relativePath(path)] = undefined;
// console.log(folderList);
folder.destroy().then(() => log(`📁➖ ${relativePath(path)}`));
folder
.destroy()
.then(() => log('info', `📁➖ ${relativePath(path)}`));
});
}
},
Expand Down
1 change: 1 addition & 0 deletions backend/filesystem/events/updateFolderHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const updateFolderHash = (folder: Folder) => {
const hash = crypto.createHash('md5').update(fileNames).digest('hex');
if (folder.folderHash != hash) {
log(
'info',
`#️⃣ Updating Folder hash: ${folder.relativePath} from ${folder.folderHash} to ${hash}`,
);
folder.folderHash = hash;
Expand Down
2 changes: 1 addition & 1 deletion backend/filesystem/fileQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const processQueue = async (action: QueueAction, payload: QueuePayload) => {
// I decided to leave them exist=false as 'archived' rather than actual delete
// await File.destroy({ where: { exists: false } });
// await Folder.destroy({ where: { exists: false } });
log('✅ Initial scan complete. Ready for changes', true);
log('info', '✅ Initial scan complete. Ready for changes', true);
break;
}
queueDone++;
Expand Down
7 changes: 4 additions & 3 deletions backend/filesystem/fileWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Op } from 'sequelize';

export const fileWatcher = async () => {
log(
'info',
'👀 Now watching: ' +
directoryPath +
(picrConfig.usePolling ? ' with POLLING' : ''),
Expand All @@ -34,18 +35,18 @@ export const fileWatcher = async () => {

watcher
.on('add', (path) => {
log('➕ ' + path);
log('info', '➕ ' + path);
addToQueue('add', { path });
})
// .on('change', path => log('✖️ ' + path))
// .on('unlink', path => log('➖ ' + path))
.on('error', (error) => console.log('⚠️ Error happened: ' + error))
.on('addDir', (path) => {
log(`📁➕ ${relativePath(path)}`);
log('info', `📁➕ ${relativePath(path)}`);
addToQueue('addDir', { path }, true);
})
.on('unlinkDir', (path) => {
log(`📁➖ ${relativePath(path)}`);
log('info', `📁➖ ${relativePath(path)}`);
addToQueue('unlinkDir', { path }, true);
})
.on('ready', () => {
Expand Down
4 changes: 2 additions & 2 deletions backend/helpers/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const zipFolder = async (folderHash: FolderHash) => {
updateZipQueue(folderHash, { status: 'Error' });
};
archive.pipe(output);
log('🗜️ Creating ZIP at path: ' + path);
log('info', '🗜️ Creating ZIP at path: ' + path);

// output.on('close', function () {
// logger('🗜️ ZIP Done: ' + path + ' ' + archive.pointer() + ' total bytes');
Expand Down Expand Up @@ -81,7 +81,7 @@ export const zipFolder = async (folderHash: FolderHash) => {
});

await archive.finalize();
log('🗜️ ZIP Completed');
log('info', '🗜️ ZIP Completed');
updateZipQueue(folderHash, { status: 'Complete' });
};

Expand Down
8 changes: 6 additions & 2 deletions backend/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const addDevLogger = () => {
}
};

export const log = (message: string, important?: boolean) => {
logger.log('info', message);
export const log = (level: LevelType, message: string, important?: boolean) => {
if (important) console.log(message); //basic "server running" type messages
logger.log(level, message);
};

// no idea where to find this type in winston :/
type LevelType = 'error' | 'warning' | 'info' | 'verbose' | 'debug';
2 changes: 1 addition & 1 deletion backend/media/generateImageThumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const generateAllThumbs = async (file: File) => {
// };

export const generateThumbnail = async (file: File, size: ThumbnailSize) => {
log(`🖼️ Generating ${size} thumbnail for ${file.name}`);
log('info', `🖼️ Generating ${size} thumbnail for ${file.name}`);
mkdirSync(dirname(thumbnailPath(file, size)), { recursive: true });
const px = thumbnailDimensions[size];

Expand Down
2 changes: 1 addition & 1 deletion backend/media/generateVideoThumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const processVideoThumbnail = async (
const outFile = thumbnailPath(file, size);
if (existsSync(outFile)) {
//lets presume thumbnails already generated if this folder exists
log('Skipping ' + file.name + ' because video cache folder exists');
log('info', 'Skipping ' + file.name + ' because video cache folder exists');
return;
}

Expand Down
4 changes: 4 additions & 0 deletions backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const picrConfig = {
tokenSecret: process.env.TOKEN_SECRET,
databaseUrl: process.env.DATABASE_URL,
debugSql: process.env.DEBUG_SQL == 'true',
consoleLogging: process.env.CONSOLE_LOGGING == 'true',
usePolling: process.env.USE_POLLING == 'true',
pollingInterval: parseInt(process.env.POLLING_INTERVAL) ?? 20,
dev: process.env.NODE_ENV === 'development',
Expand All @@ -26,6 +27,9 @@ export const picrConfig = {
if (picrConfig.dev) {
console.log('SERVER CONFIGURATION ONLY DISPLAYED IN DEV MODE');
console.log(picrConfig);
}

if (picrConfig.consoleLogging) {
addDevLogger();
}

Expand Down
2 changes: 1 addition & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ services:
### Sample `.ENV`
Copy this, make changes as necessary
```dotenv
# NOTE: You will need to restart node server after editing these as `nodemon` doesn't monitor this file
DEBUG_SQL=false
CONSOLE_LOGGING=true
USE_POLLING=true
TOKEN_SECRET=<some-long-string>
DATABASE_URL=postgres://user:pass@localhost/picr
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "backend/server.ts",
"scripts": {
"start:server-ts": "tsc -w",
"start:server": "nodemon dist/backend/server.js",
"start:server": "nodemon --watch .env --watch dist dist/backend/server.js",
"start:client": "cd frontend && npm start",
"start:db": "docker compose up -d",
"start": "concurrently npm:start:*",
Expand Down

0 comments on commit 95cd2e0

Please sign in to comment.