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

[Fix]: Stop write log to stderr, when FLAGS_logtostderr is false #2869

Merged
merged 1 commit into from
Nov 14, 2023

Conversation

NaturalSelect
Copy link
Contributor

@NaturalSelect NaturalSelect commented Nov 6, 2023

What problem does this PR solve?

Issue Number: #2811

Problem Summary:

What is changed and how it works?

What's Changed:

How it Works:

  • Set FLAGS_stderrthreshold to 3, so only FATAL log will write to stderr.

See also:Glog Doc

Effect:

┌──(nature㉿LAPTOP-9TS0FG11)-[~/curve]
└─$ docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                                                                            NAMES
af01056361a9   curvebs:unknown       "/entrypoint.sh --ro…"   13 seconds ago   Up 12 seconds                                                                                                    focused_hofstadter
55a1484d3ef6   portainer/portainer   "/portainer"             5 months ago     Up 4 hours      0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp, 9443/tcp   portainer

┌──(nature㉿LAPTOP-9TS0FG11)-[~/curve]
└─$ docker logs af01056361a9
fileList: []
WARNING: Logging before InitGoogleLogging() is written to STDERR
I 2023-11-09T13:40:54.374593+0800     1 dynamic_vlog.cpp:36] current verbose logging level is `0`
F 2023-11-09T13:40:54.375380+0800     1 main.cpp:73] For test, kill process in here
*** Check failure stack trace: ***
*** Aborted at 1699508454 (unix time) try "date -d @1699508454" if you are using GNU date ***
PC: @                0x0 (unknown)
*** SIGABRT (@0x1) received by PID 1 (TID 0x7fd991255140) from PID 1; stack trace: ***
    @     0x7fd99255a140 (unknown)
    @     0x7fd991cc8ce1 gsignal
    @     0x7fd991cb2537 abort
    @     0x55f53b861e43 google::FlushAndAbort()
    @     0x55f53b85f10a google::LogMessage::Fail()
    @     0x55f53b85f054 google::LogMessage::SendToLog()
    @     0x55f53b85e879 google::LogMessage::Flush()
    @     0x55f53b8617f2 google::LogMessageFatal::~LogMessageFatal()
    @     0x55f53b798ed0 main
    @     0x7fd991cb3d0a __libc_start_main
    @     0x55f53b79884a _start
    @                0x0 (unknown)

┌──(nature㉿LAPTOP-9TS0FG11)-[~/curve]
└─$ cat ./curvefs/docker/debian11/curvefs/mds/logs/curvefs-mds.log.INFO.20231109-134054.1 | grep "docker logs"
E 2023-11-09T13:40:54.375043+0800     1 main.cpp:70] You cann't see me in docker logs

Modified source code:

/*
 *  Copyright (c) 2021 NetEase Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*
 * Project: curve
 * Created Date: 2021-05-19
 * Author: chenwei
 */

#include <gflags/gflags.h>
#include <glog/logging.h>

#include "curvefs/src/mds/mds.h"
#include "src/common/configuration.h"
#include "curvefs/src/common/dynamic_vlog.h"

using ::curve::common::Configuration;
using ::curvefs::common::FLAGS_vlog_level;

DEFINE_string(confPath, "curvefs/conf/mds.conf", "mds confPath");
DEFINE_string(mdsAddr, "127.0.0.1:6700", "mds listen addr");

void LoadConfigFromCmdline(Configuration *conf) {
    google::CommandLineFlagInfo info;
    if (GetCommandLineFlagInfo("mdsAddr", &info) && !info.is_default) {
        conf->SetStringValue("mds.listen.addr", FLAGS_mdsAddr);
    }

    if (GetCommandLineFlagInfo("v", &info) && !info.is_default) {
        conf->SetIntValue("mds.loglevel", FLAGS_v);
    }
}

int main(int argc, char **argv) {
    // config initialization
    google::ParseCommandLineFlags(&argc, &argv, false);

    std::string confPath = FLAGS_confPath;
    auto conf = std::make_shared<Configuration>();
    conf->SetConfigPath(confPath);
    LOG_IF(FATAL, !conf->LoadConfig())
        << "load mds configuration fail, conf path = " << confPath;
    conf->GetValueFatalIfFail("mds.loglevel", &FLAGS_v);
    LoadConfigFromCmdline(conf.get());
    FLAGS_vlog_level = FLAGS_v;
    if (FLAGS_log_dir.empty()) {
        if (!conf->GetStringValue("mds.common.logDir", &FLAGS_log_dir)) {
            LOG(WARNING) << "no mds.common.logDir in " << confPath
                         << ", will log to /tmp";
        }
    }

    // initialize logging module
    // NOTE: https://github.com/google/glog#setting-flags
    FLAGS_stderrthreshold = 3;
    google::InitGoogleLogging(argv[0]);
    LOG(ERROR) << "You cann't see me in docker logs";

    conf->PrintConfig();
    LOG(FATAL) << "For test, kill process in here";

    curvefs::mds::MDS mds;

    // initialize MDS options
    mds.InitOptions(conf);

    mds.StartDummyServer();

    mds.StartCompaginLeader();

    // Initialize other modules after winning election
    mds.Init();

    // start mds server and wait CTRL+C to quit
    mds.Run();

    // stop server and background threads
    mds.Stop();

    curve::common::S3Adapter::Shutdown();
    google::ShutdownGoogleLogging();
    return 0;
}

Side effects(Breaking backward compatibility? Performance regression?):

Check List

  • Relevant documentation/comments is changed or added
  • I acknowledge that all my contributions will be made under the project's license

@NaturalSelect NaturalSelect force-pushed the master-dev branch 3 times, most recently from d8b97d4 to dd58873 Compare November 7, 2023 05:38
@NaturalSelect NaturalSelect changed the title [Fix]: Error log bug by redirect fd [Fix]: Stop write log to stderr, when FLAGS_logtostderr is false Nov 7, 2023
@NaturalSelect NaturalSelect marked this pull request as ready for review November 7, 2023 05:41
@Cyber-SiKu
Copy link
Contributor

cicheck

@NaturalSelect NaturalSelect force-pushed the master-dev branch 4 times, most recently from 7e0bab5 to eaf6536 Compare November 7, 2023 10:38
@Wine93
Copy link
Contributor

Wine93 commented Nov 7, 2023

cicheck

@Cyber-SiKu
Copy link
Contributor

Please add screenshots of the effect, including docker logs, log files, etc.

@wu-hanqing wu-hanqing merged commit fea6c06 into opencurve:master Nov 14, 2023
4 checks passed
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.

4 participants