bytesparadise / libasciidoc

A Golang library for processing Asciidoc files.
Apache License 2.0
205 stars 25 forks source link

Don't log when used as a library #1106

Closed gi-yt closed 1 year ago

gi-yt commented 1 year ago

when i use libasciidoc like a normal library, it logs to the console with information (info level) related to the conversion. Is there a way to make it not print it to the console?

        readmeReader := strings.NewReader(readmee)
        readmeWriter := &strings.Builder{}
        adocconfig := configuration.NewConfiguration(
            configuration.WithBackEnd("html5"),
            configuration.WithHeaderFooter(false))
        libasciidoc.Convert(readmeReader, readmeWriter, adocconfig)
        mightBeUnsafe = []byte(readmeWriter.String())

This code filled my console with messages like this:


INFO[0031] parsed 'image:https://godoc.org/github.com/bytesparadise/libasciidoc?status.svg["GoDoc", link="https://godoc.org/github.com/bytesparadise/libasciidoc"]
image:https://goreportcard.com/badge/github.com/bytesparadise/libasciidoc["Go Report Card", link="https://goreportcard.com/report/github.com/bytesparadise/libasciidoc"]
image:https://github.com/bytesparadise/libasciidoc/workflows/ci-build/badge.svg["GitHub Action Build Status", link="https://github.com/bytesparadise/libasciidoc/actions?query=workflow%3Aci-build"]
image:https://codecov.io/gh/bytesparadise/libasciidoc/branch/master/graph/badge.svg["Codecov", link="https://codecov.io/gh/bytesparadise/libasciidoc"]
image:https://img.shields.io/badge/License-Apache%202.0-blue.svg["License", link="https://opensource.org/licenses/Apache-2.0"]' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Libasciidoc is an open source Go library to convert from Asciidoc to HTML.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Although it does not support the full Asciidoc/Asciidoctor syntax, Libasciidoc already provides users with the following features:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Title and Sections level 1 to 6' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Document authors and revision' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Attribute declaration and substitution' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Paragraphs and admonition paragraphs' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Delimited Blocks (fenced blocks, listing blocks, example blocks, comment blocks, quoted blocks, sidebar blocks, verse blocks, open blocks)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Source code highlighting of delimited blocks (use either `chroma` or `pygments` as the `source-highlighter`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Literal blocks (paragraph starting with a space, with the `+++....+++` delimiter or with the `[literal]` attribute)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Quoted text (bold, italic, monospace, marked, superscript and subscript) and substitution prevention using the backslash (`\`) character' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Single and double quoted typographic quotes (e.g. '`single`' and "`double`")' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Explicit and implicit curved apostrophe' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Copyright (C), Registered (R), and Trademark (TM) symbols' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Passthrough (wrapping with a single plus or a triple plus, or using the `+++pass:[]+++` or `+++pass:q[]+++` macros)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'External links in paragraphs (`https://`, `http://`, `ftp://`, `irc://`, `mailto:`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Inline images in paragraphs (`image:`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Image blocks (`image::`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Icons including font, graphic icons, both in admonition blocks and inline (`icon:`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Element attributes (`ID`, `link`, `title`, `role`, etc.) including short-hand (`[#id.role1.role2]`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Ordered lists including custom numbering types (`arabic`, `upperroman`, `lowergreek`, and so forth)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Unordered lists including bullet styles' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Labeled lists, including `[horizontal]` and `[qanda]` styles' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Nesting of links of different types & attributes' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Tables (basic support: header line and cells on multiple lines, top-level table styles)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Horizontal rules (thematic breaks) and page breaks' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Table of contents' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'YAML front-matter' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'See also the link:LIMITATIONS.adoc[known limitations] page for differences between Asciidoc/Asciidoctor and Libasciidoc.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'known limitations' with 'inline_passthrough,attributes,specialchars,quotes,replacements,post_replacements' substitutions 
INFO[0031] parsed 'Further elements will be supported in the future. Feel free to open issues https://github.com/bytesparadise/libasciidoc/issues[here] to help prioritizing the upcoming work.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'here' with 'inline_passthrough,attributes,specialchars,quotes,replacements,post_replacements' substitutions 
INFO[0031] parsed 'When enabled, syntax highlighting in `[source]` blocks is backed by the https://github.com/alecthomas/chroma[Chroma libray].
The defaut class prefix is `tok-`, and it can be overridden at the document level using the `chroma-class-prefix` attribute, or from the command line interface:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Chroma libray' with 'inline_passthrough,attributes,specialchars,quotes,replacements,post_replacements' substitutions 
INFO[0031] parsed ':chroma-class-prefix: myprefix- <1>
:chroma-class-prefix: <2>' with 'callouts,specialchars' substitutions 
INFO[0031] parsed 'classes with a custom prefix' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'classes without any prefix' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '$ libasciidoc -o - -a chroma-class-prefix=myprefix- mydoc.adoc' with 'callouts,specialchars' substitutions 
INFO[0031] parsed 'You can also use the `chroma` CLI (see https://github.com/alecthomas/chroma/releases[the release page to download the binary]) to generate your CSS with the default prefix or the one of your choice:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'the release page to download the binary' with 'inline_passthrough,attributes,specialchars,quotes,replacements,post_replacements' substitutions 
INFO[0031] parsed '$ chroma -s lovelace --html --html-prefix=tok- --html-styles' with 'callouts,specialchars' substitutions 
INFO[0031] parsed 'Using `-b` (or `--backend`) the following formats are supported:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '`html5` (also `html`), this is the default' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '`xhtml5` (also `xhtml`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'To build libasciidoc and make it available on the command line, do this:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '    $ git clone https://github.com/bytesparadise/libasciidoc.git
    $ cd libasciidoc
    $ make install' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'If `$GOPATH/bin` is already in `$PATH`, then you should be good. Otherwise, for Linux and macOS users, you can run the following command:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '    $ sudo ln -s "$PWD/bin/libasciidoc" /usr/local/bin/libasciidoc' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'The libasciidoc library includes a minimalist command line interface to generate the HTML content from a given file:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '$ libasciidoc -s content.adoc' with 'callouts,specialchars' substitutions 
INFO[0031] parsed 'use `libasciidoc --help` to check all available options.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Libasciidoc provides 2 functions to convert an Asciidoc content into HTML:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Converting an `io.Reader` into an HTML document:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '    Convert(r io.Reader, output io.Writer, config *configuration.Configuration) (types.Metadata, error)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Converting a file (given its name) into an HTML document:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '    ConvertFile(output io.Writer, config *configuration.Configuration) (types.Metadata, error)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'where the returned `types.Metadata` object contains the document's title which is not part of the generated HTML `<body>` part, as well as the table of contents (even if not rendered) and other attributes of the document.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'All options/settings are passed via the `config` parameter.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'The user can define a macro by calling `renderer.WithMacroTemplate()` and passing return value to conversion functions.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed '`renderer.WithMacroTemplate()` defines a macro by the given name and associates the given template. The template is an implementation of `renderer.MacroTemplate` interface (ex. `text.Template`)' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Libasciidoc calls `Execute()` method and passes `types.UserMacro` object to template when rendering.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'An example the following:' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'var tmplStr = `<span>Example: {{.Value}}{{.Attributes.GetAsString "suffix"}}</span>`
var t = template.New("example")
var tmpl = template.Must(t.Parse(tmplStr))

output := &strings.Builder{}
content := strings.NewReader(`example::hello world[suffix=!!!!!]`)
libasciidoc.Convert(content, output, renderer.WithMacroTemplate(tmpl.Name(), tmpl))' with 'callouts,specialchars' substitutions 
INFO[0031] parsed 'Please refer to the link:CONTRIBUTE.adoc[Contribute] page.' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Contribute' with 'inline_passthrough,attributes,specialchars,quotes,replacements,post_replacements' substitutions 
INFO[0031] parsed 'Libasciidoc is available under the terms of the https://raw.githubusercontent.com/bytesparadise/libasciidoc/LICENSE[Apache License 2.0].' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] parsed 'Apache License 2.0' with 'inline_passthrough,attributes,specialchars,quotes,replacements,post_replacements' substitutions 
INFO[0031] parsed 'AsciiDoc is a trademark of the Eclipse Foundation' with 'inline_passthrough,attributes,specialchars,quotes,replacements,macros,post_replacements' substitutions 
INFO[0031] done                                          pipeline_stage=apply_substitutions
INFO[0031] time to preprocess 4608 microseconds         
INFO[0031] time to parse      12849 microseconds        
INFO[0031] time to validate   0 microseconds            
INFO[0031] time to render     1573 microseconds         
INFO[0031] total time         19031 microseconds      
xcoulon commented 1 year ago

Hello @gi-yt

Libasciidoc uses github.com/sirupsen/logrus to log messages in the console, so you may want to try something like logrus.SetLevel(logrus.ErrorLevel), provided this does not has side effect within your application or other modules it depends on.

Otherwise, I should probably create a specific logger with logrus.New() and pass it as an argument of the funcs, so we could have a custom log level for libasciidoc without interfering with the rest of the application 🤔

gi-yt commented 1 year ago

Yup! That fixed it. The program uses the standard log library so the conflict isn't an issue..

xcoulon commented 1 year ago

Yup! That fixed it. The program uses the standard log library so the conflict isn't an issue..

great! I'm glad it worked for you this way!