Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): allow to pass in custom loggers #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

line-o
Copy link
Member

@line-o line-o commented Feb 6, 2023

Add new function signature roaster:route#4 allowing to pass in a custom log function.
This allows for control what is logged where and how.

Log to stdout and stderr

declare function custom-router:log-std ($level as xs:string, $message as item()*) as empty-sequence() {
    switch(lower-case($level))
    case 'error' return util:log-system-err($message)
    default return util:log-system-out($message)
};
roaster:route($custom-router:definitions, custom-router:lookup#1, $custom-router:use, custom-router:log-std#2)

Log using util:log-app

declare function custom-router:log-app ($level as xs:string, $message as item()*) as empty-sequence() {
    util:log-app($level, "custom.log", $message)
};
roaster:route($custom-router:definitions, custom-router:lookup#1, $custom-router:use, custom-router:log-app#2)

For the above to work the logger needs to be added to log4j2.xml

<Logger name="custom.log" additivity="false" level="debug">
    <AppenderRef ref="exist.core"/><!-- or choose a different appender that you added -->
</Logger>

@line-o line-o requested a review from a team February 6, 2023 11:51
@line-o line-o added the enhancement New feature or request label Feb 6, 2023
Copy link
Contributor

@duncdrum duncdrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, and function is useful. I'm kind of missing an assertion in the testsuite that actually ensures that the custom logger is used, and the default logger is not.

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

@duncdrum Do you have an idea how to test this properly?

@duncdrum
Copy link
Contributor

duncdrum commented Feb 7, 2023

Not sure i understand your question, create a test log entry, and make sure that it only appears in the custom.log. Take a message that appears in exist.log by default, put it into custom.log and then check that the message is no longer present in exist.log ?

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

@duncdrum That's what I was looking for :) How does one access the logs of the docker container within GitHub-actions?

@duncdrum
Copy link
Contributor

duncdrum commented Feb 7, 2023

docker log ... for exist.log
docker cp ... for custom.log

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

What is the default log-level within exist-db stock images?

@duncdrum
Copy link
Contributor

duncdrum commented Feb 7, 2023

WARN i think, its in the config file in the core repo

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

It's safe to assume INFO, I guess. This would limit testability to errors at the moment.
If we would really venture into testing the custom log capability we need to alter the log4j configuration within the container. Can you point me to an example - or documentation - how to do that?

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

I think for now it would make sense to just test that certain messages are logged to exist.log and others are not.
Or can we access stdout and stderr?

@duncdrum
Copy link
Contributor

duncdrum commented Feb 7, 2023

Or can we access stdout and stderr

We can but testing the presence of certain messages in exist.log is the more important bit.

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

Yes, testing that log output is written to exist.log by default needs to be added. But for that the log4j configuration does not need to be altered.
If we have access to stdout and stderr and can make sure that the custom router logs are found there we would test both cases and would not have to go through the extra steps.

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

Aha, in the docker log configuration all messages with level INFO are directed to stdout
https://github.com/eXist-db/exist/blob/3ece92c5a1006ace8f9713d378e7393d18471043/exist-docker/src/main/xslt/log4j2-docker.xslt#L33
TIL

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

From my manual testing I could see that the message format is predictably different, though.
Would that suffice, @duncdrum ?

@duncdrum
Copy link
Contributor

duncdrum commented Feb 7, 2023

show me the message format preferably in code, and i ll tell you

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

I'll give you an example: When passing custom-router:log-std#2 to roaster:route will result in a call like

$logger("info", "[f6ad7ba0-8722-431f-ad7b-a08722e31f59] request get /jwt/public/1/2/this/is/just/a/test")

This yields following output to stdout

(Line: 72 /db/apps/roasted/modules/custom-router.xq) [f6ad7ba0-8722-431f-ad7b-a08722e31f59] request get /jwt/public/1/2/this/is/just/a/test

Each line will always start with (Line: 72 /db/apps/roasted/modules/custom-router.xq).
Output from the default logger (util:log#2) with the same message and level on the other hand

2023-02-06 12:30:36,743 [qtp77738832-38] INFO  (LogFunction.java [writeLog]:205) - (Line: -1 $EXIST_HOME/etc/../data/expathrepo/roaster-1.8.0/content/roaster.xqm) [f6ad7ba0-8722-431f-ad7b-a08722e31f59] request get /jwt/public/1/2/this/is/just/a/test

@duncdrum
Copy link
Contributor

duncdrum commented Feb 7, 2023

Looks like good testcase. Although users might actually more care about actual log files on disc.

As long as there are some tests I m happy. Whichever you are more comfortable with.

@line-o
Copy link
Member Author

line-o commented Feb 7, 2023

The whole logging facility has 0 coverage at the moment. Testing docker stdout will bring us a big leap forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants