aboutsummaryrefslogtreecommitdiff
path: root/nexus/src/main/kotlin/tech/libeufin/nexus/Log.kt
blob: 25ed95affa45380e0593089e9ce465e4704161ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2024 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

package tech.libeufin.nexus

import tech.libeufin.nexus.ebics.unzipForEach
import tech.libeufin.util.*
import java.io.*
import java.nio.file.*
import kotlin.io.path.*
import kotlin.io.*
import java.time.*

/** 
 * Log EBICS files for debugging
 * 
 * Only log if [path] is not null
 */
class FileLogger(path: String?) {
    private val dir = if (path != null) Path(path) else null

    init {
        println("$path $dir")
                if (dir != null) {
            try {
                // Create logging directory if missing
                dir.createDirectories()
            } catch (e: Exception) {
                throw Exception("Failed to init EBICS debug logging directory", e)
            }
            logger.info("Logging to '$dir'")
        }
    }

    /**
     * Logs EBICS fetch content if EBICS debug logging is enabled
     *
     * @param content EBICS fetch content
     * @param hac only true when downloading via HAC (EBICS 2)
     */
    fun logFetch(content: ByteArray, hac: Boolean = false) {
        if (dir == null) return;

        // Subdir based on current day.
        val now = Instant.now()
        val asUtcDate = LocalDate.ofInstant(now, ZoneId.of("UTC"))
        val nowMs = now.toDbMicros()
        // Creating the combined dir.
        val subDir = dir.resolve("${asUtcDate.year}-${asUtcDate.monthValue}-${asUtcDate.dayOfMonth}").resolve("fetch")
        subDir.createDirectories()
        if (hac) {
            subDir.resolve("${nowMs}_HAC_response.pain.002.xml").writeBytes(content)
        } else {
            // Write each ZIP entry in the combined dir.
            content.unzipForEach { fileName, xmlContent ->
                subDir.resolve("${nowMs}_$fileName").writeText(xmlContent)
            }
        }
    }

    /**
     * Logs EBICS submit content if EBICS debug logging is enabled
     *
     * @param content EBICS submit content
     */
    fun logSubmit(content: String) {
        if (dir == null) return;

        // Subdir based on current day.
        val now = Instant.now()
        val asUtcDate = LocalDate.ofInstant(now, ZoneId.of("UTC"))
        val nowMs = now.toDbMicros()
        // Creating the combined dir.
        val subDir = dir.resolve("${asUtcDate.year}-${asUtcDate.monthValue}-${asUtcDate.dayOfMonth}").resolve("submit")
        subDir.createDirectories()
        subDir.resolve("${nowMs}_pain.001.xml").writeText(content)
    }
}