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

log: set stderr as default log output, add .set_output_stream() #23296

Merged
merged 3 commits into from
Dec 28, 2024

Conversation

gechandesu
Copy link
Contributor

@gechandesu gechandesu commented Dec 28, 2024

This PR:

  • Adds set_output_stream() fn to log.Log struct which allows setting io.Writer as log output. It makes possible send logs to arbitrary targets such as sockets. Use cases: write logs to system journal e.g. syslog, send logs to remote server over UDP.
  • Makes STDERR the default output stream (be STDOUT before). Sending logs to STOUT is not generally used in logging libraries e.g. Go, Python stdlibs etc.
  • Edit test, since log.Log now have output_stream field with file address which can change at runtime and therefore cannot be tested in "inout" tests.

Example:

import log
import os
import net

fn main() {
	mut logger := log.Log{}
	logger.set_level(.info)
	log.set_logger(logger)

	logger.info('hello stderr')
	logger.set_output_stream(os.stdout())
	logger.info('hello stdout')

	mut udp_sock := net.dial_udp('127.0.0.1:8889')!
	logger.set_output_stream(udp_sock)
	logger.info('hello udp')
}

Screenshot From 2024-12-28 17-52-38

Huly®: V_0.6-21728

vlib/log/log.v Outdated Show resolved Hide resolved
@gechandesu gechandesu marked this pull request as draft December 28, 2024 18:39
@spytheman spytheman marked this pull request as ready for review December 28, 2024 19:51
@spytheman spytheman merged commit a47b729 into vlang:master Dec 28, 2024
1 check passed
spytheman added a commit that referenced this pull request Dec 28, 2024
@spytheman
Copy link
Member

It failed locally the valgrind test, I am reopening it after the revert.

For some reason the test was not triggered by the last commit, which led me to believe that it was fixed. I should have investigated it closer.

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

Successfully merging this pull request may close these issues.

2 participants