小编Guy*_*rin的帖子

将 PyCharm 的控制台输出格式化为 json 日志

我正在开发一个应用程序,其日志格式为 JSON,其结构类似于

{"level": "INFO", "lineno": 85, "timestamp": "2020-01-01", "message": "Some message"}
Run Code Online (Sandbox Code Playgroud)

这种格式在日志发送到 ELK 的生产中非常有效,但随着日志变得越来越复杂,在 PyCharm 中理解它们会变得更加困难。

我正在寻找一种方法将控制台的输出格式化(这样我就不必更改代码中日志的格式)为更具可读性,例如

2020-01-01 INFO 85 Some message
Run Code Online (Sandbox Code Playgroud)

在寻找一些解决方案时,我发现了这个选项bunyan,并且还建议使用 GrepConsole。我没能让他们中的任何一个工作。

bunyan解决方案根本不起作用。日志一直显示为 JSON,GrepConsole 非常适合过滤或突出显示某些文本,但不适合操作。

任何想法将不胜感激。

python intellij-idea pycharm

7
推荐指数
1
解决办法
782
查看次数

Guice 多个注释

我有一个名为 的接口StatsStore。我有这家商店的 2 个实现。InMemoryStatsStore一个名为and的内存中 SQL 实现SqlStatsStore。为了注入它们,我创建了 2 个注释@InMemoryStore@SqlStore. 注射是:

bind(StatsStore.class)
    .annotatedWith(InMemoryStore.class)
    .to(InMemoryStatsStore.class);

bind(StatsStore.class)
    .annotatedWith(SqlStore.class)
    .to(SqlStatsStore.class);   
Run Code Online (Sandbox Code Playgroud)

现在我想添加一层新的注释来分隔 和 ,InMemoryStringStoreInMemoryNumberStore我无法向绑定行添加多个注释,例如以下内容无法编译:

bind(StatsStore.class)
    .annotatedWith(InMemoryStore.class)
    .annotatedWith(NumberStoreAnnotation.class) // using named doesn't work as well
    .to(InMemoryNumberStore.class);  
Run Code Online (Sandbox Code Playgroud)

如何在不使用单个命名注释的情况下添加多个注释,添加的层越多,这将变得相当复杂?

我想到的另一个解决方案是注入两次:

bind(StatsStore.class)
    .annotatedWith(InMemoryStore.class)
    .to(InMemoryStatsStore.class);

 bind(InMemoryStatsStore.class)
    .annotatedWith(NumberStoreAnnotation.class)
    .to(InMemoryNumberStore.class);
Run Code Online (Sandbox Code Playgroud)

谢谢大家。

java guice

5
推荐指数
1
解决办法
4551
查看次数

嵌套期货未执行

我遇到了一个奇怪的情况。我正在摆弄CompletableFuture,当运行以下代码时,我得到了意想不到的结果:

public static void main(String[] args) {     
    CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<Object>>>>>> completableFutureCompletableFuture = CompletableFuture.supplyAsync(() -> {
        System.out.println("first");
        return CompletableFuture.supplyAsync(() -> {
            System.out.println("second");
            return CompletableFuture.supplyAsync(() -> {
                System.out.println("third");
                return CompletableFuture.supplyAsync(() -> {
                    System.out.println("fourth");
                    return CompletableFuture.supplyAsync(() -> {
                        System.out.println("fifth");
                        return CompletableFuture.completedFuture(null);
                    });
                });
            });
        });
    });

   completableFutureCompletableFuture.get();
}
Run Code Online (Sandbox Code Playgroud)

没有抛出异常(即使使用时exceptionally),我看到的是控制台输出是

first
second
third // appears sometimes
Run Code Online (Sandbox Code Playgroud)

现在,显然这段代码没有真正的生产价值,但这代表了一种情况,即您的代码具有未知数量的嵌套,其中每个或其中一些嵌套创建的嵌套CompleteableFutures将不会被执行。

任何解释(以及如何修复的示例)将不胜感激

java multithreading completable-future

5
推荐指数
2
解决办法
2416
查看次数

在Gradle中获取AWS角色凭证

我有一个S3要在构建过程中访问的存储库。它包含我项目的某些依赖项。我的项目已部署到具有指定角色-的EC2实例Repo_dependent。该角色具有Access_Repo附加的策略:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "Stmt1484560548000",
        "Effect": "Allow",
        "Action": [
            "s3:GetObject",
            "s3:ListBucket",
            "s3:GetBucketLocation"
        ],
        "Resource": [
            "arn:aws:s3:::my_bucket",
            "arn:aws:s3:::my_bucket/*"
        ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

部署新服务器时,出现The AWS Access Key Id you provided does not exist in our records. (Service: Amazon S3; Status Code: 403; Error Code: InvalidAccessKeyId; Request ID: 02169BFDCF7AFE10)异常。

我的构建脚本就是这个(为简单起见缩写)

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.amazonaws:aws-java-sdk:1.11.83'
  }
}

import com.amazonaws.auth.*

repositories {
  jcenter()
  maven {
    url "s3://my_bucket.s3.amazonaws.com" …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 amazon-ec2 gradle amazon-iam aws-sdk

3
推荐指数
1
解决办法
3098
查看次数