Set logging level in glog package - go

How can I set logging level for golang glog package to ERROR.
example.go:
package main
import (
"github.com/golang/glog"
"flag"
)
func main() {
flag.Parse()
glog.Info("Info level")
glog.Error("Error level")
glog.Flush()
}
$ go run example.go -logtostderr=true -stderrthreshold=ERROR
I1214 15:46:00.743002 13429 example.go:10] Info level
E1214 15:46:00.743211 13429 example.go:11] Error level
In the above example I have set the stderrthreshold flag to ERROR but am still getting INFO level logs. I want only ERROR level logs.

By default, glog will write
all log messages to log files (they are created in /tmp by default);
log messages with a severity of stderrthreshold or above to stderr.
By using the -logtostderr=true flag this behaviour is changed:
Logs are written to standard error instead of to files.
no log messages are written to log files;
all log messages are written to stderr.
The stderrthreshold presumably has no effect when -logtostderr=true is used. And in your invocation it didn't have an effect anyway, because the default threshold is already ERROR.
To summarize, by just running go run example.go without any command line arguments, only log messages with severity ERROR or above are written to stderr, as you desired.

Related

Golang following container logs undeclared name Log

I'm trying to build a container with testcontainers and output the logs to stdout.
I have followed their documentation (https://golang.testcontainers.org/features/follow_logs/) but when writing the Accept function, I get an error:
func (g *TestLogConsumer) Accept(l Log) {
g.Msgs = append(g.Msgs, string(l.Content))
}
The problem is that I didn't import a certain package, so I can't use "Log".
I get the following error:
undeclared name: Log
The question is, which package should I import. How can I make vscode import these packages automatically for me ? I have installed the go extension, but it doesn't import anything automatically.

gotext: extract failed: pipeline: golang.org/x/text/message is not imported

I am trying to run the following command from within my template.go file:
//go:generate gotext -srclang=en update -out=catalog.go -lang=en,de_DE,es_MX,fr_CA,pt_BR
I am expected to get a catalog.go generated, but instead, I get the following error:
gotext: extract failed: pipeline: golang.org/x/text/message is not imported
template.go:3: running "gotext": exit status 1
I do have the following import in the template.go after the generate command:
import (
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/text/message"
)
I've tried to move the import before the generate command. I've also tried to run generate ./... from within the root of the project. I've also tried to run gotext by itself, but it's the same error message.
I also found the following thread on github:
https://github.com/golang/go/issues/26312
I've tried what was suggested there, but it didn't seem to have solved the issue either.
I have solved the issue by running rm -rf vendor/golang.org/x/text command from the root of the project. Of course for things to work, I also needed to have gotext installed. This can be done by running go get golang.org/x/text/cmd/gotext.
I believe the issue could be solved if binaries of .../text/message are installed in the GOPATH/bin as well

Recieving following error

I am recieving an error while compilation.
Root cause is the file has import of context package, but however during compilation github.com/docker/docker/vendor/golang.org/x/net/context is getting referred a, any pointers on as how to make it to refer to context import instead of docker's vendor package
Summarizing the issue,
import in the file is as follows
import "golang.org/x/net/context"
there is a method call which takes context object as an argument, however, there are two repositories which have context ,
golang.org/x/net/context
github.com/docker/docker/vendor/golang.org/x/net/context
During compilation based on length go is selecting the 2 repository. But method needs the 1st one golang.org/x/net/context. hence the type error in code snippet is recieved
"-X main.version=dev-49-gc8cc01b -X main.commit=c8cc01b -X main.branch=master" ./...
github.com/influxdata/telegraf/plugins/inputs/docker
plugins/inputs/docker/docker.go:103: cannot use c (type *client.Client) as type DockerClient in assignment:
*client.Client does not implement DockerClient (wrong type for ContainerList method)
have ContainerList("github.com/docker/docker/vendor/golang.org/x/net/context".Context, types.ContainerListOptions) ([]types.Container, error)
want ContainerList("context".Context, types.ContainerListOptions) ([]types.Container, error) make: *** [build] Error 2 sh-4.2# vi plugins/inputs/docker/docker.go
Two options I can think of:
Add an alias for the context import, e.g.
goContext golang.org/x/net/context
docContext github.com/docker/docker/vendor/golang.org/x/net/context
Break the context dependencies into two files, or packages

Show Python exception errors in Heroku log

I have a Heroku application written i Python. When it fails it does not log anything, but just stops code execution at the location of failure (it does not execute print statements just below) and then continues to run as nothing happens.
How can I display exception errors and traceback in the log? Is the behaviour different when exceptions are raised using the raise statement?
I have set the following in the config file:
DEBUG = True
PRESERVE_CONTEXT_ON_EXCEPTION = True
Tried with and without PRESERVE_CONTEXT_ON_EXCEPTION.
It depends on your environment.
Try to add these lines:
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
app.logger.addHandler(stream_handler)
Also, if you run the server with Gunicorn, you should add --log-level debug to your Procfile

How can I disable console messages when running maven commands?

I'm in the process of executing Maven commands to run tests in the console (MacOSX). Recently, development efforts have produced extraneous messages in the console (info, debug, warning, etc.) I'd like to know how to remove messages like this:
INFO c.c.m.s.c.p.ApplicationProperties - Loading application properties from: app-config/shiro.properties
I've used this code to remove messages from the dbunit tests:
ch.qos.logback.classic.Logger Logger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger("org.dbunit");
Logger.setLevel(Level. ERROR);
However, I'm unsure how to disable these additional (often verbose and irritating) messages from showing up on the console so that I can see the output more easily. Additional messages appear as above and these:
DEBUG c.c.m.s.c.f.dao.AbstractDBDAO - Adding filters to the Main Search query.
WARN c.c.m.s.c.p.JNDIConfigurationProperties - Unable to find JNDI value for name: xxxxx
INFO c.c.m.a.t.d.DatabaseTestFixture - * executing sql: xxxxx
The successful answer was:
SOLUTION: Solution to issue IS adding a 'logback-test.xml' file to the root of my test folder. I used the default contents (as instructed by the documentation - thanks #Charlie). Once file exists there, FIXED!

Resources