aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/logging/phiLogging.scala
diff options
context:
space:
mode:
authorvlad <vlad@driver.xyz>2017-10-31 11:09:29 -0700
committervlad <vlad@driver.xyz>2017-10-31 11:09:29 -0700
commit18c68db6e446c48c5aed0010a1d057368273685c (patch)
treee31d84c7bfbf62baabab34ef8c6d18b018ada55a /src/main/scala/xyz/driver/core/logging/phiLogging.scala
parent0cb06d70bd91e1e6a4ab9d97851ef9db7aaedfd6 (diff)
downloaddriver-core-18c68db6e446c48c5aed0010a1d057368273685c.tar.gz
driver-core-18c68db6e446c48c5aed0010a1d057368273685c.tar.bz2
driver-core-18c68db6e446c48c5aed0010a1d057368273685c.zip
PHI-safe logging from PDS UIphi-safe-logging
Diffstat (limited to 'src/main/scala/xyz/driver/core/logging/phiLogging.scala')
-rw-r--r--src/main/scala/xyz/driver/core/logging/phiLogging.scala57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/logging/phiLogging.scala b/src/main/scala/xyz/driver/core/logging/phiLogging.scala
new file mode 100644
index 0000000..27b6ddc
--- /dev/null
+++ b/src/main/scala/xyz/driver/core/logging/phiLogging.scala
@@ -0,0 +1,57 @@
+package xyz.driver.core.logging
+
+import java.time.{LocalDateTime, ZoneId}
+
+import org.slf4j.LoggerFactory
+import xyz.driver.core.Id
+import xyz.driver.core.auth.User
+import xyz.driver.core.logging.phi._
+
+trait PhiSafeLogger {
+
+ def error(message: NoPhiString): Unit
+
+ def warn(message: NoPhiString): Unit
+
+ def info(message: NoPhiString): Unit
+
+ def debug(message: NoPhiString): Unit
+
+ def trace(message: NoPhiString): Unit
+
+}
+
+class DefaultPhiSafeLogger(underlying: org.slf4j.Logger) extends PhiSafeLogger {
+
+ def error(message: NoPhiString): Unit = underlying.error(message.text)
+
+ def warn(message: NoPhiString): Unit = underlying.warn(message.text)
+
+ def info(message: NoPhiString): Unit = underlying.info(message.text)
+
+ def debug(message: NoPhiString): Unit = underlying.debug(message.text)
+
+ def trace(message: NoPhiString): Unit = underlying.trace(message.text)
+
+}
+
+trait PhiSafeLogging {
+
+ protected val logger: PhiSafeLogger = new DefaultPhiSafeLogger(LoggerFactory.getLogger(getClass.getName))
+
+ /**
+ * Logs the failMessage on an error level, if isSuccessful is false.
+ * @return isSuccessful
+ */
+ protected def loggedError(isSuccessful: Boolean, failMessage: NoPhiString): Boolean = {
+ if (!isSuccessful) {
+ logger.error(failMessage)
+ }
+ isSuccessful
+ }
+
+ protected def logTime(userId: Id[User], label: NoPhiString, obj: NoPhiString): Unit = {
+ val now = LocalDateTime.now(ZoneId.of("Z"))
+ logger.info(noPhi"User id=${NoPhi(userId)} performed an action at $label=$now with a $obj")
+ }
+}