diff options
author | Steffen Ritter | 2024-07-12 11:04:56 +0200 |
---|---|---|
committer | Steffen Ritter | 2024-07-12 11:04:56 +0200 |
commit | c3a840618c066d14937f6b6a615949bdedca485f (patch) | |
tree | ea6de172d548ed0c4b1608adb68bb99d375c25c4 | |
parent | [server] Also log user on upload/download (diff) | |
download | tutor-module-c3a840618c066d14937f6b6a615949bdedca485f.tar.gz tutor-module-c3a840618c066d14937f6b6a615949bdedca485f.tar.xz tutor-module-c3a840618c066d14937f6b6a615949bdedca485f.zip |
[server] Change logging again...
We already write most of the interesting dozmod actions to the sat-db.
So instead of miserably duplicating only some of them to the logging
facility, just write everything to both backends.
Journalctl is also easier to search and grep for admins.
6 files changed, 23 insertions, 51 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLog.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLog.java index 27240582..b5a04eec 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLog.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLog.java @@ -7,6 +7,7 @@ import org.apache.logging.log4j.Logger; import org.openslx.bwlp.sat.database.Database; import org.openslx.bwlp.sat.database.MysqlConnection; import org.openslx.bwlp.sat.database.MysqlStatement; +import org.openslx.bwlp.sat.util.Formatter; import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.util.QuickTimer; import org.openslx.util.QuickTimer.Task; @@ -19,14 +20,17 @@ public class DbLog { /** * Add entry to logging table. * - * @param userId user causing the action (can be null) + * @param user user causing the action (can be null) * @param targetId object being acted upon (userid, lectureid, imageid, or * null) * @param description Human readable description of the action being * performed */ - public static void log(final String userId, final String targetId, final String description) { + public static void log(final UserInfo user, final String targetId, final String description) { final long timeStamp = Util.unixTime(); + final String userName = (user == null ? "System" : Formatter.userFullName(user)); + + LOGGER.info("[" + userName + "] " + description); QuickTimer.scheduleOnce(new Task() { @Override public void fire() { @@ -35,9 +39,9 @@ public class DbLog { + " (dateline, userid, targetid, description) VALUES" + " (:dateline, :userid, :targetid, :description)"); stmt.setLong("dateline", timeStamp); - stmt.setString("userid", userId); + stmt.setString("userid", user == null ? null : user.userId); stmt.setString("targetid", targetId); - stmt.setString("description", description == null ? "" : description); + stmt.setString("description", description == null ? "" : "[" + userName + "] " + description); stmt.executeUpdate(); connection.commit(); } catch (SQLException e) { @@ -47,8 +51,4 @@ public class DbLog { }); } - public static void log(UserInfo user, String targetId, String description) { - log(user == null ? null : user.userId, targetId, description); - } - } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/IncomingDataTransfer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/IncomingDataTransfer.java index 61a2cfc4..9e92f84c 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/IncomingDataTransfer.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/IncomingDataTransfer.java @@ -291,7 +291,7 @@ public class IncomingDataTransfer extends IncomingTransferBase { versionSettings, getChunks(), machineDescription); versionWrittenToDb.set(true); } - DbLog.log(owner, image.imageBaseId, "Successfully uploaded new version " + getVersionId() + DbLog.log(owner, image.imageBaseId, "successfully uploaded new version " + getVersionId() + " of VM '" + image.imageName + "'"); } catch (SQLException e) { LOGGER.error("Error finishing upload: Inserting version to DB failed", e); @@ -376,7 +376,7 @@ public class IncomingDataTransfer extends IncomingTransferBase { if (chunk.getFailCount() > 3) { cancel(); errorMessage = "Uploaded file is corrupted - did you modify the VM while uploading?"; - DbLog.log(owner, image.imageBaseId, "Server is cancelling upload of Version " + getVersionId() + DbLog.log(owner, image.imageBaseId, "Server is cancelling upload of version " + getVersionId() + " for '" + image.imageName + "': Hash check for block " + chunk.getChunkIndex() + " failed " + chunk.getFailCount() + " times. Maybe the user was still running the VM when starting the upload."); diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/SyncTransferHandler.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/SyncTransferHandler.java index 3d38f99a..51b5f838 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/SyncTransferHandler.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/fileserv/SyncTransferHandler.java @@ -130,20 +130,9 @@ public class SyncTransferHandler { TAuthorizationException, TTransferRejectedException { TransferInformation transferInfo; OutgoingDataTransfer existing = uploadsByVersionId.get(imgVersion.imageVersionId); - String user = userToken; - try { - UserInfo userInfo = ThriftManager.getMasterClient().getUserFromToken(userToken); - if (userInfo != null) { - user = userInfo.getFirstName() + " " + userInfo.getLastName() + " (" + userInfo.getUserId() + ")"; - } - } catch (TInvalidTokenException e) { - e.printStackTrace(); - } catch (TException e) { - e.printStackTrace(); - } if (existing != null) { - LOGGER.info(user + " wants to upload image " + imgVersion.imageVersionId + " of '" + imgBase.imageName + "' to master" - + " which is already in progess via " + existing.getId()); + LOGGER.info("Upload of image " + imgVersion.imageVersionId + " of '" + imgBase.imageName + "' to master" + + " is already in progess via " + existing.getId()); return existing.getId(); } File absFile = FileSystem.composeAbsoluteImagePath(imgVersion); @@ -200,8 +189,6 @@ public class SyncTransferHandler { OutgoingDataTransfer transfer = new OutgoingDataTransfer(transferInfo, absFile, imgVersion.imageVersionId); uploadsByVersionId.put(imgVersion.imageVersionId, transfer); uploadsByTransferId.put(transfer.getId(), transfer); - LOGGER.info(user + " wants to upload image " + imgVersion.imageVersionId + " of '" + imgBase.imageName + "' to master" - + ", created transfer " + transfer.getId()); heartBeatTask.fire(); return transfer.getId(); } @@ -209,17 +196,6 @@ public class SyncTransferHandler { public synchronized static String requestImageDownload(String userToken, ImagePublishData image) throws TInvocationException, TAuthorizationException, TNotFoundException { TransferInformation transferInfo; - String user = userToken; - try { - UserInfo userInfo = ThriftManager.getMasterClient().getUserFromToken(userToken); - if (userInfo != null) { - user = userInfo.getFirstName() + " " + userInfo.getLastName() + " (" + userInfo.getUserId() + ")"; - } - } catch (TInvalidTokenException e) { - e.printStackTrace(); - } catch (TException e) { - e.printStackTrace(); - } // Already replicating this one? IncomingDataTransfer existing = downloads.get(image.imageVersionId); if (existing != null) @@ -266,8 +242,6 @@ public class SyncTransferHandler { localImageData != null); downloads.put(transfer.getId(), transfer); heartBeatTask.fire(); - LOGGER.info(user + " wants to download image " + image.imageVersionId + " of '" + image.imageName + "' from master" - + ", created transfer " + transfer.getId()); return transfer.getId(); } catch (FileNotFoundException e) { LOGGER.warn("Could not open " + tmpFile.getAbsolutePath()); diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldImages.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldImages.java index d07332a5..79843c02 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldImages.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldImages.java @@ -144,7 +144,7 @@ public class DeleteOldImages implements Runnable { writeln(sb, version.imageVersionId, ": Cannot delete image: ", e.getMessage()); } writeln(sb, version.imageVersionId, ": OK"); - DbLog.log((String)null, version.imageBaseId, + DbLog.log(null, version.imageBaseId, "Version " + version.imageVersionId + " (" + Formatter.date(version.createTime) + ") deleted from database and storage."); } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java index 579333bb..4f0e634b 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/DeleteOldUsers.java @@ -68,7 +68,7 @@ public class DeleteOldUsers implements Runnable { for (UserInfo user : inactiveUsers) { try { if (DbUser.deleteUser(user)) { - DbLog.log((String)null, null, "Deleted inactive user " + Formatter.userFullName(user)); + DbLog.log(null, null, "Deleted inactive user " + Formatter.userFullName(user)); } } catch (SQLException e) { // Already logged diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java index ad99b2cf..01c8b792 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java @@ -323,7 +323,7 @@ public class ServerHandler implements SatelliteServer.Iface { throw new TInvocationException(InvocationError.INVALID_DATA, "Invalid or empty name"); try { String imageBaseId = DbImage.createImage(user, imageName); - DbLog.log(user, imageBaseId, "New Image created: '" + imageName + "'"); + DbLog.log(user, imageBaseId, "created image '" + imageName + "'"); return imageBaseId; } catch (SQLException e) { throw new TInvocationException(); @@ -408,7 +408,7 @@ public class ServerHandler implements SatelliteServer.Iface { DbImage.getBaseIdForVersionId(imageVersionId)); DbImage.markForDeletion(imageVersionId); DbImage.setDeletion(DeleteState.WANT_DELETE, imageVersionId); - DbLog.log(user, imageSummary.imageBaseId, Formatter.userFullName(user) + " deleted Version " + DbLog.log(user, imageSummary.imageBaseId, "deleted version " + imageVersionId + " of '" + imageSummary.imageName + "' (" + imageSummary.imageBaseId + ")"); } catch (SQLException e) { @@ -450,7 +450,7 @@ public class ServerHandler implements SatelliteServer.Iface { } DeleteOldImages.hardDeleteImages(); } - DbLog.log(user, imageDetails.imageBaseId, Formatter.userFullName(user) + " deleted Image '" + imageDetails.imageName + DbLog.log(user, imageDetails.imageBaseId, "deleted image '" + imageDetails.imageName + "' with all its versions (" + index + ")"); try { DbImage.deleteBasePermanently(imageBaseId); @@ -497,7 +497,7 @@ public class ServerHandler implements SatelliteServer.Iface { } DbImage.setImageOwner(imageBaseId, newOwnerId, user); UserInfo newOwner = DbUser.getOrNull(newOwnerId); - DbLog.log(user, imageBaseId, Formatter.userFullName(user) + " changed owner of '" + DbLog.log(user, imageBaseId, "changed owner of '" + imageSummary.imageName + "' to " + Formatter.userFullName(newOwner)); DbLog.log(user, newOwnerId, Formatter.userFullName(newOwner) + " was declared new owner of '" + imageSummary.imageName + "' by " + Formatter.userFullName(user)); @@ -577,8 +577,7 @@ public class ServerHandler implements SatelliteServer.Iface { } imagePublishData.uploader = user; String transferId = SyncTransferHandler.requestImageDownload(userToken, imagePublishData); - DbLog.log(user, imagePublishData.imageBaseId, Formatter.userFullName(user) - + " triggered download from master server of version " + imageVersionId + " of '" + DbLog.log(user, imagePublishData.imageBaseId, "triggered download from master server of version " + imageVersionId + " of '" + imagePublishData.imageName + "'"); return transferId; } @@ -601,8 +600,7 @@ public class ServerHandler implements SatelliteServer.Iface { User.canUploadToMasterOrFail(user, imgBase); try { String transferId = SyncTransferHandler.requestImageUpload(userToken, imgBase, imgVersion); - DbLog.log(user, imgBase.imageBaseId, Formatter.userFullName(user) - + " triggered upload to master server of version " + imageVersionId + " of '" + DbLog.log(user, imgBase.imageBaseId, "triggered upload to master server of version " + imageVersionId + " of '" + imgBase.imageName + "'"); return transferId; } catch (TTransferRejectedException e) { @@ -627,7 +625,7 @@ public class ServerHandler implements SatelliteServer.Iface { Sanitizer.handleLectureDates(lecture, null); try { String lectureId = DbLecture.create(user, lecture); - DbLog.log(user, lectureId, Formatter.userFullName(user) + " created lecture '" + lecture.lectureName + "'"); + DbLog.log(user, lectureId, "created lecture '" + lecture.lectureName + "'"); return lectureId; } catch (SQLException e) { throw new TInvocationException(); @@ -696,7 +694,7 @@ public class ServerHandler implements SatelliteServer.Iface { LectureSummary lecture = DbLecture.getLectureSummary(user, lectureId); if (!DbLecture.delete(lectureId)) throw new TNotFoundException(); - DbLog.log(user, lectureId, Formatter.userFullName(user) + " deleted lecture '" + lecture.lectureName + "'"); + DbLog.log(user, lectureId, "deleted lecture '" + lecture.lectureName + "'"); } catch (SQLException e) { throw new TInvocationException(); } @@ -741,7 +739,7 @@ public class ServerHandler implements SatelliteServer.Iface { try { DbLecture.setOwner(user, lectureId, newOwnerId); UserInfo newOwner = DbUser.getOrNull(newOwnerId); - DbLog.log(user, lectureId, Formatter.userFullName(user) + " changed owner of '" + DbLog.log(user, lectureId, "changed owner of '" + lecture.lectureName + "' to " + Formatter.userFullName(newOwner)); DbLog.log(user, newOwnerId, Formatter.userFullName(newOwner) + " was declared new owner of '" + lecture.lectureName + "' by " + Formatter.userFullName(user)); |