-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changing log level #1527
Comments
I see this doc for --zap-level, but it talks about command line command against containers & I cannot find any example on how to use it: https://github.com/operator-framework/operator-sdk/blob/master/doc/user/logging.md |
@mossuchida The If you didn't change these lines in your |
I am newbie to Go lang (I am using it for 3 ~ 4 weeks), I am trying differently, but so far I am not successful. Would you provide me a code sample? I am using default logger. |
@mossuchida Did you use Here's a much simpler snippet with just the pieces important for logging: package main
import (
"github.com/operator-framework/operator-sdk/pkg/log/zap"
"github.com/spf13/pflag"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
var globalLog = logf.Log.WithName("global")
func main() {
// Add the zap logger flag set to the CLI. The flag set must
// be added before calling pflag.Parse().
pflag.CommandLine.AddFlagSet(zap.FlagSet())
pflag.Parse()
// Use a zap logr.Logger implementation. If none of the zap
// flags are configured (or if the zap flag set is not being
// used), this defaults to a production zap logger.
//
// The logger instantiated here can be changed to any logger
// implementing the logr.Logger interface. This logger will
// be propagated through the whole operator, generating
// uniform and structured logs.
logf.SetLogger(zap.Logger())
scopedLog := logf.Log.WithName("scoped")
globalLog.Info("Printing at INFO level")
globalLog.V(1).Info("Printing at DEBUG level")
scopedLog.Info("Printing at INFO level")
scopedLog.V(1).Info("Printing at DEBUG level")
} Default log level: $ go run main.go
{"level":"info","ts":1559866292.307987,"logger":"global","msg":"Printing at INFO level"}
{"level":"info","ts":1559866292.308039,"logger":"scoped","msg":"Printing at INFO level"} Debug log level: $ go run main.go --zap-level=1
{"level":"info","ts":1559866310.065048,"logger":"global","msg":"Printing at INFO level"}
{"level":"debug","ts":1559866310.0650969,"logger":"global","msg":"Printing at DEBUG level"}
{"level":"info","ts":1559866310.065119,"logger":"scoped","msg":"Printing at INFO level"}
{"level":"debug","ts":1559866310.065123,"logger":"scoped","msg":"Printing at DEBUG level"} |
@joelanford Should we add this to the examples in the logging? I got this question at least twice before, so might make sense. :) |
I am running in kubernetes env (IKS), I am assuming it is using Dockerfile in build dir & want to see less logs in the deployed env (not in my local), but this is what I see. Would you let me know What to change? I just want to change it to print error case only in deployed pod logs using "kubectl logs pod-name" (I added some info logs, but I don't want to see them) Dockerfile has these lines to startup operator SDK, but I don't have user_setup file locally (it seems like it comes with the image). How do you pass --operator-flags='--zap-level=10' in Dockerfile? RUN /usr/local/bin/user_setup |
@mossuchida Rather than changing the docker image via the Dockerfile, I'd suggest that you set args:
- '--zap-level=error' This args field should be in the container spec for your operator. Here's the full example based on a basic apiVersion: apps/v1
kind: Deployment
metadata:
name: memcached-operator
spec:
replicas: 1
selector:
matchLabels:
name: memcached-operator
template:
metadata:
labels:
name: memcached-operator
spec:
serviceAccountName: memcached-operator
containers:
- name: memcached-operator
# Replace this with the built image name
image: REPLACE_IMAGE
command:
- memcached-operator
args:
- '--zap-level=error'
imagePullPolicy: Always
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: OPERATOR_NAME
value: "memcached-operator"
|
Thanks, --zap-level=error worked. Now I am changing the log from reqLogger.Info to reqLogger.Debug, but it complains that it doesn't exist. I tried to use reqLogger.V(1).Info with "--zap-level=info", but it prints out reqLogger.V(1).Info also. Would you provide complete --zap-level options & reqLogger options that I can use for operator.yaml & controller.go? |
@mossuchida the logging doc describes the
So you can use These levels correspond to the logr interface as follows:
Also to address your questions:
Correct, see the logr interface to see what functions are available on the reqLogger.
That shouldn't be happening. Is it possible that you are overriding the log level somewhere? If so, can you reproduce it based on the simple snippet code from my earlier comment? |
Thanks, it worked! It looks like I was not logged into docker & image push was denied, there were no V(1) in the code. |
Please let me know how to change the log level. Currently, all the logs are displayed. I tried the following, but it complained: syntax error: non-declaration statement outside function body
Error: failed to build operator binary
atom := logf.Log.NewAtomicLevel()
atom.SetLevel(logf.Log.ErrorLevel)
var log = logf.Log.WithName("controller_moss")
The text was updated successfully, but these errors were encountered: