Formatting code and log snippets in posts

If you are including code and log snippets in a post, consider if there are ways to make these more readable using some of the available formatting options. Well formatted posts will help other users understand and engage with your discussion.

Some quick tips for improved formatting:

  • Include short code and log snippets as inline text rather than images. Text is searchable for other users who might encounter a similar issue or need the exact text to help investigate the issue you are experiencing. The trust level for new users (Seedling) limits the number of attachments and links to encourage inline posting.

  • Add triple backticks (```) before and after a snippet of code. This syntax (aka code fencing in GItHub-flavoured Markdown) will automatically detect language formatting and generally be an improvement over a straight copy & paste.

  • Log lines can be formatted better (and visually distinguished) by prefixing with > (aka quoting). This will wrap long lines and provide some visual distinction from code blocks. The editing toolbar has a shortcut for toggling line quoting on the currently selected text: . Note: you can also use the (```) formatting for log snippets, but quoting generally works better.

  • Add spacing to break up long blocks of text and improve readability

  • If you are planning to share lengthy code or log snippets, consider whether you might be able to provide a more concise extract. In some cases a link to a gist on GitHub or a Pastebin service might be easier to share.

Before & after: code fencing (```)

db.inventory.insertMany([ { item: “journal”, qty: 25, status: “A”, size: { h: 14, w: 21, uom: “cm” }, tags: [ “blank”, “red” ] }, { item: “notebook”, qty: 50, status: “A”, size: { h: 8.5, w: 11, uom: “in” }, tags: [ “red”, “blank” ] }, { item: “paper”, qty: 10, status: “D”, size: { h: 8.5, w: 11, uom: “in” }, tags: [ “red”, “blank”, “plain” ] }, { item: “planner”, qty: 0, status: “D”, size: { h: 22.85, w: 30, uom: “cm” }, tags: [ “blank”, “red” ] }, { item: “postcard”, qty: 45, status: “A”, size: { h: 10, w: 15.25, uom: “cm” }, tags: [ “blue” ] } ]);

db.inventory.insertMany([
   { item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] },
   { item: "paper", qty: 10, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] },
   { item: "planner", qty: 0, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
]);

Before & after: quoting and line spacing

I’m seeing a message like this logged every minute:
2019-09-17T17:19:53.618-0400 I NETWORK [conn1] received client metadata from 127.0.0.1:54552 conn1: { application: { name: “MongoDB Shell” }, driver: { name: “MongoDB Internal Client”, version: “4.0.12” }, os: { type: “Darwin”, name: “Mac OS X”, architecture: “x86_64”, version: “18.7.0” } }

I’m seeing a message like this logged every minute:

2019-09-17T17:19:53.618-0400 I NETWORK [conn1] received client metadata from 127.0.0.1:54552 conn1: { application: { name: “MongoDB Shell” }, driver: { name: “MongoDB Internal Client”, version: “4.0.12” }, os: { type: “Darwin”, name: “Mac OS X”, architecture: “x86_64”, version: “18.7.0” } }

12 Likes