Skip to content

Commit

Permalink
refactor: simplify logger implementation and LogComponent to selfcont…
Browse files Browse the repository at this point in the history
…ain log level

Signed-off-by: Cristian G <cristian.castro@iohk.io>
  • Loading branch information
cristianIOHK committed Sep 16, 2024
1 parent 6b5ebf8 commit 81dd24e
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import org.hyperledger.identus.walletsdk.edgeagent.protocols.issueCredential.Off
import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation.PresentationSubmission
import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation.RequestPresentation
import org.hyperledger.identus.walletsdk.domain.models.KeyValue
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerMock
import org.hyperledger.identus.walletsdk.logger.LoggerMock
import org.hyperledger.identus.walletsdk.mercury.ApiMock
import org.hyperledger.identus.walletsdk.pollux.PolluxImpl
import org.hyperledger.identus.walletsdk.pollux.models.AnonCredential
Expand Down Expand Up @@ -114,7 +114,7 @@ class AnoncredsTests {
connectionManager = connectionManager,
seed = null,
api = ApiMock(statusCode = HttpStatusCode.OK, response = "{\"success\":\"true\"}"),
logger = EdgeAgentLoggerMock()
logger = LoggerMock()
)

val attachmentDescriptor =
Expand Down Expand Up @@ -192,7 +192,7 @@ class AnoncredsTests {
connectionManager = connectionManager,
seed = null,
api = ApiMock(HttpStatusCode.OK, "{\"success\":\"true\"}"),
logger = EdgeAgentLoggerMock()
logger = LoggerMock()
)

val attachmentDescriptor =
Expand Down Expand Up @@ -263,7 +263,7 @@ class AnoncredsTests {
connectionManager = connectionManagerMock,
seed = seed,
api = apiMock,
logger = EdgeAgentLoggerMock()
logger = LoggerMock()
)

val credential = AnonCredential.fromStorableData(
Expand Down Expand Up @@ -355,7 +355,7 @@ class AnoncredsTests {
connectionManager = connectionManagerMock,
seed = seed,
api = apiMock,
logger = EdgeAgentLoggerMock()
logger = LoggerMock()
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ import org.hyperledger.identus.walletsdk.domain.models.keyManagement.SeedKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.StorableKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.StorablePrivateKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.TypeKey
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLogger
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.Logger
import org.hyperledger.identus.walletsdk.logger.LoggerImpl
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.logger.LogLevel
import kotlin.math.log

/**
* Apollo defines the set of cryptographic operations that are used in the Atala PRISM.
*/
class ApolloImpl(
private val logger: EdgeAgentLogger = EdgeAgentLoggerImpl(LogComponent.APOLLO)
val logger: Logger = LoggerImpl(LogComponent.APOLLO)
) : Apollo {

/**
Expand All @@ -57,6 +59,7 @@ class ApolloImpl(
* @return An array of mnemonic phrases.
*/
override fun createRandomMnemonics(): Array<String> {
LogComponent.APOLLO.logLevel = LogLevel.INFO
return MnemonicHelper.createRandomMnemonics().toTypedArray()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import org.hyperledger.identus.walletsdk.domain.models.keyManagement.PublicKey
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.logger.LogLevel
import org.hyperledger.identus.walletsdk.logger.Metadata
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLogger
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.Logger
import org.hyperledger.identus.walletsdk.logger.LoggerImpl

/**
* Castor is a powerful and flexible library for working with DIDs. Whether you are building a decentralised application
Expand All @@ -36,7 +36,7 @@ class CastorImpl
@JvmOverloads
constructor(
val apollo: Apollo,
private val logger: EdgeAgentLogger = EdgeAgentLoggerImpl(LogComponent.CASTOR)
private val logger: Logger = LoggerImpl(LogComponent.CASTOR)
) : Castor {
var resolvers: Array<DIDResolver> = arrayOf(
LongFormPrismDIDResolver(this.apollo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import org.hyperledger.identus.walletsdk.domain.models.DIDUrl
import org.hyperledger.identus.walletsdk.domain.models.OctetPublicKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.KeyPair
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.PublicKey
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.LoggerImpl
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.logger.LogLevel
import org.hyperledger.identus.walletsdk.logger.Metadata
Expand All @@ -64,7 +64,7 @@ internal class CastorShared {
*
* @property logger The logger instance.
*/
private val logger = EdgeAgentLoggerImpl(LogComponent.CASTOR)
private val logger = LoggerImpl(LogComponent.CASTOR)

/**
* parseDID parses a string representation of a Decentralized Identifier (DID) into a DID object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation
import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation.RequestPresentation
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.logger.Metadata
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLogger
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.Logger
import org.hyperledger.identus.walletsdk.logger.LoggerImpl
import org.hyperledger.identus.walletsdk.pluto.PlutoBackupTask
import org.hyperledger.identus.walletsdk.pluto.PlutoRestoreTask
import org.hyperledger.identus.walletsdk.pluto.models.backup.BackupV0_0_1
Expand Down Expand Up @@ -153,7 +153,7 @@ open class EdgeAgent {
private val edgeAgentScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
private val api: Api
internal var connectionManager: ConnectionManager
private var logger: EdgeAgentLogger
private var logger: Logger
private val agentOptions: AgentOptions

/**
Expand Down Expand Up @@ -181,7 +181,7 @@ open class EdgeAgent {
connectionManager: ConnectionManager,
seed: Seed?,
api: Api?,
logger: EdgeAgentLogger = EdgeAgentLoggerImpl(LogComponent.EDGE_AGENT),
logger: Logger = LoggerImpl(LogComponent.EDGE_AGENT),
agentOptions: AgentOptions = AgentOptions()
) {
edgeAgentScope.launch {
Expand Down Expand Up @@ -235,7 +235,7 @@ open class EdgeAgent {
seed: Seed? = null,
api: Api? = null,
mediatorHandler: MediationHandler,
logger: EdgeAgentLogger = EdgeAgentLoggerImpl(LogComponent.EDGE_AGENT),
logger: Logger = LoggerImpl(LogComponent.EDGE_AGENT),
agentOptions: AgentOptions = AgentOptions()
) {
edgeAgentScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ private const val METADATA_PRIVACY_STR = "------"
private val hashingLog = UUID.randomUUID().toString()

/**
* EdgeAgentLogger is an interface that defines methods for logging messages
* Logger is an interface that defines methods for logging messages
* with different log levels and metadata.
*/
interface EdgeAgentLogger {
interface Logger {
/**
* Logs a debug message with optional metadata.
*
Expand Down Expand Up @@ -66,26 +66,23 @@ interface EdgeAgentLogger {
* @param error The error to be logged.
* @param metadata An array of metadata objects to be included in the log message. Defaults to an empty array if not provided.
*
* @see EdgeAgentLogger.error
* @see Logger.error
* @see Metadata
*/
fun error(error: Error, metadata: Array<Metadata> = arrayOf())
}

/**
* Implementation of the EdgeAgentLogger interface.
* Implementation of the Logger interface.
*
* @property category the LogComponent category for this logger
*/
class EdgeAgentLoggerImpl(category: LogComponent, private var logLevel: LogLevel = LogLevel.INFO, tag: String? = null) :
EdgeAgentLogger {
class LoggerImpl(private val category: LogComponent) :
Logger {

private val log = logging(
if (tag != null) {
"$tag.$category"
} else {
"[org.hyperledger.identus.walletsdk.$category]"
}
"[${category::class.qualifiedName}.$category]"

)

/**
Expand All @@ -95,7 +92,7 @@ class EdgeAgentLoggerImpl(category: LogComponent, private var logLevel: LogLevel
* @param metadata An array of metadata objects associated with the message (optional).
*/
override fun debug(message: String, metadata: Array<Metadata>) {
if (logLevel != LogLevel.NONE) {
if (category.logLevel != LogLevel.NONE) {
log.debug { message }
if (metadata.isNotEmpty()) {
log.debug { "Metadata: ${arrayToString(metadata)}" }
Expand All @@ -110,7 +107,7 @@ class EdgeAgentLoggerImpl(category: LogComponent, private var logLevel: LogLevel
* @param metadata An array of metadata objects to be associated with the information message.
*/
override fun info(message: String, metadata: Array<Metadata>) {
if (logLevel != LogLevel.NONE) {
if (category.logLevel != LogLevel.NONE) {
log.info { message }
if (metadata.isNotEmpty()) {
log.info { "Metadata: ${arrayToString(metadata)}" }
Expand All @@ -125,7 +122,7 @@ class EdgeAgentLoggerImpl(category: LogComponent, private var logLevel: LogLevel
* @param metadata An array of metadata objects associated with the warning message.
*/
override fun warning(message: String, metadata: Array<Metadata>) {
if (logLevel != LogLevel.NONE) {
if (category.logLevel != LogLevel.NONE) {
log.warn { message }
if (metadata.isNotEmpty()) {
log.warn { "Metadata: ${arrayToString(metadata)}" }
Expand All @@ -140,7 +137,7 @@ class EdgeAgentLoggerImpl(category: LogComponent, private var logLevel: LogLevel
* @param metadata An array of metadata objects to be associated with the error message (optional).
*/
override fun error(message: String, metadata: Array<Metadata>) {
if (logLevel != LogLevel.NONE) {
if (category.logLevel != LogLevel.NONE) {
log.error { message }
if (metadata.isNotEmpty()) {
log.error { "Metadata: ${arrayToString(metadata)}" }
Expand All @@ -155,7 +152,7 @@ class EdgeAgentLoggerImpl(category: LogComponent, private var logLevel: LogLevel
* @param metadata An array of metadata objects associated with the error (optional).
*/
override fun error(error: Error, metadata: Array<Metadata>) {
if (logLevel != LogLevel.NONE) {
if (category.logLevel != LogLevel.NONE) {
log.error { error.message }
if (metadata.isNotEmpty()) {
log.error { "Metadata: ${arrayToString(metadata)}" }
Expand All @@ -170,7 +167,7 @@ class EdgeAgentLoggerImpl(category: LogComponent, private var logLevel: LogLevel
* @return The converted String representation of the Metadata objects, with each object's value separated by a new line.
*/
private fun arrayToString(array: Array<Metadata>): String {
return array.joinToString { "${it.getValue(logLevel)}\n" }
return array.joinToString { "${it.getValue(category.logLevel)}\n" }
}
}

Expand Down Expand Up @@ -340,11 +337,11 @@ enum class LogLevel(val value: Int) {
* - POLLUX
* - EDGE_AGENT
*/
enum class LogComponent {
APOLLO,
CASTOR,
MERCURY,
PLUTO,
POLLUX,
EDGE_AGENT
enum class LogComponent(var logLevel: LogLevel) {
APOLLO(LogLevel.DEBUG),
CASTOR(LogLevel.DEBUG),
MERCURY(LogLevel.DEBUG),
PLUTO(LogLevel.DEBUG),
POLLUX(LogLevel.DEBUG),
EDGE_AGENT(LogLevel.DEBUG)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import org.hyperledger.identus.walletsdk.domain.models.DIDDocument
import org.hyperledger.identus.walletsdk.domain.models.KeyValue
import org.hyperledger.identus.walletsdk.domain.models.MercuryError
import org.hyperledger.identus.walletsdk.domain.models.Message
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLogger
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.Logger
import org.hyperledger.identus.walletsdk.logger.LoggerImpl
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.logger.LogLevel
import org.hyperledger.identus.walletsdk.logger.Metadata
Expand Down Expand Up @@ -55,7 +55,7 @@ constructor(
private val castor: Castor,
private val protocol: DIDCommProtocol,
private val api: Api,
private val logger: EdgeAgentLogger = EdgeAgentLoggerImpl(LogComponent.MERCURY)
private val logger: Logger = LoggerImpl(LogComponent.MERCURY)
) : Mercury {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import org.hyperledger.identus.walletsdk.domain.models.AttachmentDescriptor
import org.hyperledger.identus.walletsdk.domain.models.DID
import org.hyperledger.identus.walletsdk.domain.models.MercuryError
import org.hyperledger.identus.walletsdk.domain.models.Message
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.LoggerImpl
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.logger.LogLevel
import org.hyperledger.identus.walletsdk.logger.Metadata
Expand All @@ -56,7 +56,7 @@ class DIDCommWrapper(castor: Castor, pluto: Pluto, apollo: Apollo) : DIDCommProt
private val didDocResolver = DIDCommDIDResolver(castor)
private val secretsResolver = DIDCommSecretsResolver(pluto, apollo)
private val didComm = DIDComm(didDocResolver, secretsResolver)
private val logger = EdgeAgentLoggerImpl(LogComponent.MERCURY)
private val logger = LoggerImpl(LogComponent.MERCURY)

/**
* Converts a JSON element to a map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import org.hyperledger.identus.walletsdk.domain.models.keyManagement.JWK
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.PrivateKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.StorableKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.StorablePrivateKey
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLogger
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.Logger
import org.hyperledger.identus.walletsdk.logger.LoggerImpl
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.pluto.models.backup.BackupV0_0_1
import org.hyperledger.identus.walletsdk.pluto.data.DbConnection
Expand All @@ -60,7 +60,7 @@ import org.hyperledger.identus.walletsdk.pluto.data.StorableCredential as Storab
*/
class PlutoImpl(
private val connection: DbConnection,
private val logger: EdgeAgentLogger = EdgeAgentLoggerImpl(LogComponent.PLUTO)
private val logger: Logger = LoggerImpl(LogComponent.PLUTO)
) : Pluto {
private var db: SdkPlutoDb? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation
import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation.PresentationSubmissionOptions
import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation.PresentationSubmissionOptionsAnoncreds
import org.hyperledger.identus.walletsdk.edgeagent.protocols.proofOfPresentation.PresentationSubmissionOptionsJWT
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLogger
import org.hyperledger.identus.walletsdk.logger.EdgeAgentLoggerImpl
import org.hyperledger.identus.walletsdk.logger.Logger
import org.hyperledger.identus.walletsdk.logger.LoggerImpl
import org.hyperledger.identus.walletsdk.logger.LogComponent
import org.hyperledger.identus.walletsdk.pluto.RestorationID
import org.hyperledger.identus.walletsdk.pollux.models.AnonCredential
Expand All @@ -118,7 +118,7 @@ open class PolluxImpl(
val apollo: Apollo,
val castor: Castor,
private val api: Api = ApiImpl(httpClient()),
private val logger: EdgeAgentLogger = EdgeAgentLoggerImpl(LogComponent.POLLUX)
private val logger: Logger = LoggerImpl(LogComponent.POLLUX)
) : Pollux {

/**
Expand Down
Loading

0 comments on commit 81dd24e

Please sign in to comment.