You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/12/27 07:56:01 UTC

[GitHub] [pulsar-client-cpp] shibd opened a new pull request, #158: The C API supports setting the log level.

shibd opened a new pull request, #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158

   ### Motivation
   #137
   
   ### Modifications
   - Set the default log level of C to `DEBUG`. When the user sets the custom `logger`, users can filter the log level according to the level field.
   
   Such as:
   ``` c
   void custom_logger(pulsar_logger_level_t level, const char *file, int line, const char *message,
                 void *ctx) {
       // Control the log level yourself.
       if (level >= pulsar_INFO) {
            printf("[%u] [%s] [%d] [%s] \n", level, file, line, message);
       }
   }
   ```
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc-required` 
   (Your PR needs to update docs and you will update later)
   
   - [x] `doc-not-needed` 
   (Please explain why)
   
   - [ ] `doc` 
   (Your PR contains doc changes)
   
   - [ ] `doc-complete`
   (Docs have been already added)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1057566143


##########
examples/SampleCustomLoggerCApi.c:
##########
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#include <pulsar/c/client.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+void format_time(char *output){
+    time_t rawtime;
+    struct tm * timeinfo;
+
+    time(&rawtime);
+    timeinfo = localtime(&rawtime);
+
+    sprintf(output, "%d %d %d %d:%d:%d",
+            timeinfo->tm_year + 1900, timeinfo->tm_mon + 1,  timeinfo->tm_mday,
+            timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
+}
+
+void custom_logger(pulsar_logger_level_t level, const char *file, int line, const char *message,
+              void *ctx) {
+    time_t mytime = time(NULL);
+    char * time_str = ctime(&mytime);
+    // Control the log level yourself.
+    if (level >= pulsar_DEBUG) {
+        format_time(time_str);
+        printf("[%s] [%u] [%s] [%d] [%s] \n", time_str, level, file, line, message);
+    }

Review Comment:
   Though it's an example, we should check `level` first to avoid `time` and `ctime` calls.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#issuecomment-1367319138

   I will merge this PR for now. It's only included in the next 3.2.0 release. If I have a better idea, I will open a PR to remove the new API in this PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower merged pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
BewareMyPower merged PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#issuecomment-1365760970

   Not related to this PR, I found the `.c` file is not formatted by `clang-format`. I think you can fix the code style first. The `clang-format` check could be fixed in another PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] RobertIndie commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
RobertIndie commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1058091225


##########
lib/c/c_ClientConfiguration.cc:
##########
@@ -78,7 +78,7 @@ class PulsarCLogger : public pulsar::Logger {
     PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
         : file_(file), logger_(logger), ctx_(ctx) {}
 
-    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
+    bool isEnabled(Level level) { return true; }

Review Comment:
   Could we support setting the level in the PulsarCLogger. Like SimpleLogger here: https://github.com/apache/pulsar-client-cpp/blob/5d5ed2acc2e89aed90b593343369f360644440e7/lib/SimpleLogger.h#L53
   Otherwise, all debug output-related calculations will be executed, which may have some impact on the performance.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] shibd commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
shibd commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1058101752


##########
lib/c/c_ClientConfiguration.cc:
##########
@@ -78,7 +78,7 @@ class PulsarCLogger : public pulsar::Logger {
     PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
         : file_(file), logger_(logger), ctx_(ctx) {}
 
-    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
+    bool isEnabled(Level level) { return true; }

Review Comment:
   For C users, We expose a method: `pulsar_logger `.
   
   https://github.com/apache/pulsar-client-cpp/blob/0e86e9f1f8304fd0bbf21185c1c66d0b5ac58482/include/pulsar/c/client_configuration.h#L36-L37
   
   `True` is always returned here in order to let user controls what level of the log is printed in this method.
   
   Such as:
   
   ``` c
   void custom_logger(pulsar_logger_level_t level, const char *file, int line, const char *message,
                 void *ctx) {
       // Control the log level yourself.
       if (level >= pulsar_INFO) {
            printf("[%u] [%s] [%d] [%s] \n", level, file, line, message);
       }
   }
   ```
   
   This approach avoids the need to expose additional interfaces to set the log level.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] shibd commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
shibd commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1058140664


##########
lib/c/c_ClientConfiguration.cc:
##########
@@ -78,7 +78,7 @@ class PulsarCLogger : public pulsar::Logger {
     PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
         : file_(file), logger_(logger), ctx_(ctx) {}
 
-    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
+    bool isEnabled(Level level) { return true; }

Review Comment:
   Good point. I will change the solution.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] RobertIndie commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
RobertIndie commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1058137971


##########
lib/c/c_ClientConfiguration.cc:
##########
@@ -78,7 +78,7 @@ class PulsarCLogger : public pulsar::Logger {
     PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
         : file_(file), logger_(logger), ctx_(ctx) {}
 
-    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
+    bool isEnabled(Level level) { return true; }

Review Comment:
   Take this as an example:
   https://github.com/apache/pulsar-client-cpp/blob/5d5ed2acc2e89aed90b593343369f360644440e7/lib/MessageCrypto.cc#L153-L157
   In this case, lines 154-156 will always be executed even though the user doesn't want to get the debug log. We usually use this approach to avoid executing some performance-critical code when we don't need to print the debug log.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] RobertIndie commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
RobertIndie commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1058091225


##########
lib/c/c_ClientConfiguration.cc:
##########
@@ -78,7 +78,7 @@ class PulsarCLogger : public pulsar::Logger {
     PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
         : file_(file), logger_(logger), ctx_(ctx) {}
 
-    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
+    bool isEnabled(Level level) { return true; }

Review Comment:
   Could we support setting the level in the PulsarCLogger. Like SimpleLogger here: https://github.com/apache/pulsar-client-cpp/blob/5d5ed2acc2e89aed90b593343369f360644440e7/lib/SimpleLogger.h#L53
   Otherwise, all debug output-related calculations will be called, which may have some impact on the performance.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1057565667


##########
lib/c/c_ClientConfiguration.cc:
##########
@@ -78,7 +78,7 @@ class PulsarCLogger : public pulsar::Logger {
     PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
         : file_(file), logger_(logger), ctx_(ctx) {}
 
-    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
+    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_DEBUG; }

Review Comment:
   We can just return true here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] shibd commented on a diff in pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
shibd commented on code in PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#discussion_r1058154970


##########
lib/c/c_ClientConfiguration.cc:
##########
@@ -78,7 +78,7 @@ class PulsarCLogger : public pulsar::Logger {
     PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
         : file_(file), logger_(logger), ctx_(ctx) {}
 
-    bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
+    bool isEnabled(Level level) { return true; }

Review Comment:
   @RobertIndie I added `pulsar_client_configuration_set_logger_and_level` to  change log level, PTAL.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#issuecomment-1367318428

   The design might not be good, I think how to configure the logger might need more considerations. It's better to make the configuration more flexible.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] shibd commented on pull request #158: The C API supports setting the log level.

Posted by GitBox <gi...@apache.org>.
shibd commented on PR #158:
URL: https://github.com/apache/pulsar-client-cpp/pull/158#issuecomment-1365900169

   > Not related to this PR, I found the `.c` file is not formatted by `clang-format`. I think you can fix the code style first. The `clang-format` check could be fixed in another PR.
   
   Thanks for the reminder. I open a new PR #159 fix it. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org